PhoneGap中的navigator.notification.alert不起作用。

12
标题已经很清楚了,但我仍然不明白原因。
来源: 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');
    }
};

在android/res/xml中的config.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>

还有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>

我试图找出为什么它不工作,但过去几个小时一直没有成功。 我相对较新于phonegap,所以如果我错过了什么,请告诉我...

PS. Phonegap版本:3.0.0-0.14.3,设备sdk 8,AVD sdk 8(两者结果相同)

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

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

PhoneGap样例应用程序在Android上无法工作navigator.notification.*失败 "navigator.notification [undefined]不是对象"中所述。

原因是:很可能是index.html中的phonegap*js或cordova*js名称与assets/www目录中的文件名不匹配。但是我只有platforms/android/assets/www中的phonegap.js和cordova.js,并且使用完全相同的名称进行包含...仍然无法弄清楚原因...

更新

如果我进行远程构建,则应用程序可以正常工作,但是如果进行本地构建,则无法正常工作,尽管我已安装了版本3所述的插件。

6个回答

7
我使用 Cordova CLI 3.0.9 构建了您的项目,使用命令行工具并遵循此处列出的文档 (http://cordova.apache.org/docs/en/edge/cordova_notification_notification.md.html#Notification)。我使用了您的 HTML 和 JS 代码,对话框弹出得很好。
在查看其他文件时,我注意到 config.xml 中存在差异;我的 config.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="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>

请注意,我的代码中有 org.apache.cordova.dialogs.Notification - 你的代码缺少了 dialogs 命名空间,这是故意的吗?如果加上 "dialogs" 命名空间,能否正常工作?如果使用最新的 CLI 版本重新构建呢?

嗯,没错,但如果我没记错的话,我已经把它们粘贴进去并尝试运行了,但没有成功。让我再测试一下,然后告诉你。 - 0x_Anakin
你升级了 Cordova CLI 吗?如果是这样,它是否将 value="org.apache.cordova.dialogs.Notification" /> 添加到 config.xml 中? - MBillau
现在一切都正常了。看起来是 Cordova 安装出了问题。我将其完全删除并重新安装,现在没有任何问题。显然这就是为什么 config.xml 不像应该的那样的原因。 - 0x_Anakin

2
我使用的是Visual Studio 2015,并通过添加“Notification插件”解决了这个问题。
双击您项目中的config.xml文件,选择“Notification插件”并安装。 enter image description here

2
您只需将此功能添加到您的项目中... 进入phonegap项目文件夹(而不是platform文件夹),例如:cd MobileAplications/MyPhoneGapExample 然后,添加插件(我安装了cordova而不是phonegap,所以我假设如果您安装了phonegap,则必须使用“phonegap”命令):cordova plugin add org.apache.cordova.dialogs
希望对您有所帮助!
附注: 如果有人不知道平台文件夹和项目文件夹之间的区别,请参阅以下文档:http://docs.phonegap.com/en/3.2.0/guide_cli_index.md.html#The%20Command-Line%20Interface

0

尝试将此功能添加到您的config.xml文件中

 <feature name="Notification">
  <param name="wp-package" value="Notification"/>
</feature>

...并建议将config.xml文件复制到您的www目录。


0
重新安装设备上的APK应用程序。只有当系统安装时,系统才会同意权限。仅重建是不起作用的。重新安装后,权限将在屏幕上显示并激活。

0

您只能在Android模拟器或设备上看到工作效果,在Ripple中无法使用。

    var app = {
    // Application Constructor
    initialize: function() {        
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        //this.receivedEvent('deviceready');
            console.log(navigator.notification);

            navigator.notification.alert(
                    'You are the winner!',  // message
                    alertDismissed,         // callback
                    'Game Over',            // title
                    'Done'                  // buttonName
                );
            //navigator.notification.beep(2);
    },

    // 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);
    }
};app.initialize();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接