phonegap navigator.notification.alert不起作用

时间:2013-09-16 12:22:45

标签: cordova notifications

标题是自我解释的,我不知道为什么。

来源: 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>

我试图弄清楚为什么它在过去几小时没有运气的情况下无效。 我是最新的手机,所以请如果我错过了什么让我知道......

PS。 Phonegap -version:3.0.0-0.14.3,device sdk 8,AVD sdk 8(两个结果相同)

将代码包装在try catch中后,

更新我收到了以下错误消息:

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

AS PhoneGap sample application for android does not work和。{ navigator.notification.* fails "navigator.notification [undefined] is not an object"

原因是:Likely the name of phonegap*js or cordova*js in your index.html does not match the file name in the assets/www directory.但是我在platform / android / assets / www中只有phonegap.js和cordova.js而且我包含了完全相同的名字......仍然无法想象这个出...

更新

如果我进行远程构建,应用程序可以正常使用本地构建它没有;但是我已经安装了版本3所述的插件

6 个答案:

答案 0 :(得分:6)

我使用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名称空间缺失了。这是故意的吗?如果添加“对话框”命名空间,它是否有效?如果使用最新的CLI版本重建,该怎么办?

答案 1 :(得分:2)

您所要做的就是将此功能添加到您的项目中...... 站起来对准phonegap项目文件夹(而不是平台文件夹),例如:cd MobileAplications / MyPhoneGapExample 然后,添加插件(我安装了cordova而不是关闭phonegap,所以如果你安装phonegap我必须使用“phonegap”命令):cordova plugin add org.apache.cordova.dialogs

我希望它对你有用!!

PD: 如果有人不知道平台文件夹和项目文件夹之间的区别,这里是可以显示差异的文档:http://docs.phonegap.com/en/3.2.0/guide_cli_index.md.html#The%20Command-Line%20Interface

答案 2 :(得分:2)

我正在使用visual studio 2015,我通过添加Notification插件解决了这个问题。

双击项目中的config.xml,然后选择Notification plugin并安装。

enter image description here

答案 3 :(得分:0)

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

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

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

答案 4 :(得分:0)

你只会看到从android模拟器或设备工作,在涟漪不起作用。

    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();

答案 5 :(得分:0)

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