phonegap navigator.notification.alert 不起作用 [英] phonegap navigator.notification.alert doesn't work

查看:23
本文介绍了phonegap navigator.notification.alert 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题不言自明,我不知道为什么.

来源:www/index.html:

<头><meta charset="utf-8"/><meta name="format-detection" content="telephone=no"/><meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=设备-dpi"/><link rel="stylesheet" type="text/css" href="css/index.css"/><title>Hello World</title><身体><div class="app"><h1>PhoneGap</h1><div id="deviceready" class="blink"><p class="事件监听">连接到设备</p><p class="event received">设备准备就绪</p>

<script type="text/javascript" src="phonegap.js"></script><script type="text/javascript" src="js/index.js"></script><script type="text/javascript">应用程序初始化();

index.js:

var app = {//应用程序构造器初始化:函数(){this.bindEvents();},//绑定事件监听器////绑定启动时需要的任何事件.常见事件有://加载"、设备就绪"、离线"和在线".绑定事件:函数(){document.addEventListener('deviceready', this.onDeviceReady, false);},//设备就绪事件处理程序////'this' 的作用域是事件.为了调用'receivedEvent'//函数,我们必须显式调用 'app.receivedEvent(...);'onDeviceReady:函数(){app.receivedEvent('deviceready');},//在收到的事件上更新 DOM接收事件:函数(ID){var parentElement = document.getElementById(id);var listenElement = parentElement.querySelector('.listening');var receivedElement = parentElement.querySelector('.received');listenElement.setAttribute('style', 'display:none;');receivedElement.setAttribute('style', 'display:block;');console.log('收到的事件:' + id);navigator.notification.alert('PhoneGap Alert', null, 'Title', 'Button');}};

android/res/xml 中的 config.xml:

<content src="index.html"/><功能名称="应用程序"><param name="android-package" value="org.apache.cordova.App"/></功能><功能名称="通知"><param name="android-package" value="org.apache.cordova.Notification"/></功能><access origin="http://127.0.0.1*"/><preference name="useBrowserHistory" value="true"/><preference name="exit-on-suspend" value="false"/><preference name="permissions" value="none"/><preference name="orientation" value="default"/><preference name="target-device" value="universal"/><preference name="fullscreen" value="true"/><preference name="webviewbounce" value="true"/><preference name="prerendered-icon" value="true"/><preference name="stay-in-webview" value="false"/><preference name="ios-statusbarstyle" value="black-opaque"/><preference name="detect-data-types" value="true"/><preference name="show-splash-screen-spinner" value="true"/><preference name="auto-hide-splash-screen" value="true"/><preference name="disable-cursor" value="false"/><preference name="android-minSdkVersion" value="8"/><preference name="android-installLocation" value="auto"/></小部件>

和androidmanifest.xml:

我想弄清楚为什么它在过去几个小时内没有运气就不起作用.我是 phonegap 的新手,所以如果我遗漏了什么,请告诉我...

附注.Phonegap -version : 3.0.0-0.14.3 , device sdk 8 , AVD sdk 8 (两者结果相同)

更新 将代码包装在 try catch 中后,我收到此错误消息:

'navigator.notification' undefined 表达式的结果不是一个对象

PhoneGap android 示例应用程序中提到的AS 不起作用navigator.notification.* 失败 "navigator.notification [undefined] 不是对象"

原因是:可能你的index.html中phonegap*js或cordova*js的名字与assets/www目录下的文件名不匹配.但我只有phonegap.js和平台/android/assets/www 中的cordova.js,我包含完全相同的名称......仍然无法弄清楚......

更新

如果我进行远程构建,则应用程序在本地构建时可以正常工作;但我已经安装了版本 3 中提到的插件

解决方案

我使用 Cordova CLI 3.0.9,使用命令行工具并遵循此处列出的文档(http://cordova.apache.org/docs/en/edge/cordova_notification_notification.md.html#Notification).我使用了你的 HTML 和 JS 代码,对话框弹出得很好.

在查看其他文件时,我注意到 config.xml 的不同;我的 config.xml 看起来像这样:

<content src="index.html"/><功能名称="应用程序"><param name="android-package" value="org.apache.cordova.App"/></功能><功能名称="振动"><param name="android-package" value="org.apache.cordova.vibration.Vibration"/></功能><功能名称="通知"><param name="android-package" value="org.apache.cordova.dialogs.Notification"/></功能><access origin="*"/><preference name="useBrowserHistory" value="true"/><preference name="exit-on-suspend" value="false"/><preference name="fullscreen" value="true"/><preference name="webviewbounce" value="true"/></小部件>

请注意,我的具有 org.apache.cordova.dialogs.Notification - 由于某种原因,您的缺少 dialogs 命名空间.这是故意的吗?如果添加对话框"命名空间,它是否有效?如果您使用最新的 CLI 版本重建呢?

The title is self explanatory, I ca't figure out why tho.

sources: www/index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>PhoneGap</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

index.js:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);

        navigator.notification.alert('PhoneGap Alert', null, 'Title', 'Button');
    }
};

config.xml in android/res/xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
    <name>Hello Cordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <feature name="App">
        <param name="android-package" value="org.apache.cordova.App" />
    </feature>
    <feature name="Notification">
        <param name="android-package" value="org.apache.cordova.Notification" />
    </feature>
    <access origin="http://127.0.0.1*" />
    <preference name="useBrowserHistory" value="true" />
    <preference name="exit-on-suspend" value="false" />
    <preference name="permissions" value="none" />
    <preference name="orientation" value="default" />
    <preference name="target-device" value="universal" />
    <preference name="fullscreen" value="true" />
    <preference name="webviewbounce" value="true" />
    <preference name="prerendered-icon" value="true" />
    <preference name="stay-in-webview" value="false" />
    <preference name="ios-statusbarstyle" value="black-opaque" />
    <preference name="detect-data-types" value="true" />
    <preference name="show-splash-screen-spinner" value="true" />
    <preference name="auto-hide-splash-screen" value="true" />
    <preference name="disable-cursor" value="false" />
    <preference name="android-minSdkVersion" value="8" />
    <preference name="android-installLocation" value="auto" />
</widget>

and androidmanifest.xml:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.acs.acs_mobile" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="main" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />
</manifest>

I'm trying to figure out why it doesn't work with no luck for the past hours. I'm realtivley new to phonegap so please If I'm missing something let me know...

PS. Phonegap -version : 3.0.0-0.14.3 , device sdk 8 , AVD sdk 8 (both same result)

Update after wrapping the code inside a try catch I got this error message:

result of expression 'navigator.notification' undefined is not an object

AS mentioned in PhoneGap sample application for android does not work and navigator.notification.* fails "navigator.notification [undefined] is not an object"

The cause is : Likely the name of phonegap*js or cordova*js in your index.html does not match the file name in the assets/www directory. but I only have phonegap.js and cordova.js in platforms/android/assets/www and I'm including with the exact same name... Still can't figure this out...

UPDATE

If I do a remote build the app is working ok with local build it doesn;t altho I have installed the plugins as mentioned for version 3

解决方案

I built your project using Cordova CLI 3.0.9, using the command line tools and following the docs listed here (http://cordova.apache.org/docs/en/edge/cordova_notification_notification.md.html#Notification). I used your HTML and JS code and the dialog popped up just fine.

When looking through the other files, I noticed a difference in config.xml; my config.xml looks like this:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
    <name>Hello Cordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <feature name="App">
        <param name="android-package" value="org.apache.cordova.App" />
    </feature>
    <feature name="Vibration">
        <param name="android-package" value="org.apache.cordova.vibration.Vibration" />
    </feature>
    <feature name="Notification">
        <param name="android-package" value="org.apache.cordova.dialogs.Notification" />
    </feature>
    <access origin="*" />
    <preference name="useBrowserHistory" value="true" />
    <preference name="exit-on-suspend" value="false" />
    <preference name="fullscreen" value="true" />
    <preference name="webviewbounce" value="true" />
</widget>

Notice that mine has org.apache.cordova.dialogs.Notification - yours is missing the dialogs namespace for some reason. Is this on purpose? If you add "dialogs" namespace, does it work? What about if you rebuild using the latest CLI version?

这篇关于phonegap navigator.notification.alert 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆