【问题标题】:phonegap navigator.notification.alert doesn't workphonegap navigator.notification.alert 不起作用
【发布时间】:2013-09-16 12:22:45
【问题描述】:

标题不言自明,我不明白为什么。

来源: 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 的新手,所以如果我遗漏了什么,请告诉我...

附言。 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

正如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. 但我在platforms/android/assets/www 中只有phonegap.js 和cordova.js,而且我包含的名称完全相同...仍然无法弄清楚。 ..

更新

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

【问题讨论】:

    标签: cordova notifications


    【解决方案1】:

    我使用 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 版本重建会怎样?

    【讨论】:

    • 嗯是正确的,但如果我没记错的话,我把那些粘贴在那里并试图运行它但没有运气。让我再测试一下,我会告诉你的。
    • 您是否升级了 Cordova CLI?如果是这样,是否在config.xml中添加value="org.apache.cordova.dialogs.Notification" /&gt;
    • 现在一切正常。看来这是一个损坏的科尔多瓦安装。我完全删除它并重新安装,我现在有 0 个问题。显然这就是为什么 config.xml 不是它应该的样子。
    【解决方案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

    【讨论】:

      【解决方案3】:

      我使用的是visual studio 2015,我通过添加通知插件解决了这个问题。

      双击项目中的 config.xml,然后选择通知插件并安装。

      【讨论】:

        【解决方案4】:

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

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

        ...以及将 config.xml 文件复制到 www 目录的好建议。

        【讨论】:

          【解决方案5】:

          您只会看到在 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();
          

          【讨论】:

            【解决方案6】:

            在设备上重新安装 APK 应用程序。只有在安装系统时,系统才同意权限。只有重建不起作用。重新安装后,权限会显示在屏幕上并处于活动状态。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多