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 -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 android 示例应用程序中提到的那样不起作用,并且 navigator.notification.* 失败“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 中提到的插件

4

6 回答 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.Notificationdialogs -由于某种原因,您的缺少名称空间。这是故意的吗?如果添加“对话框”命名空间,它会起作用吗?如果您使用最新的 CLI 版本重建会怎样?

于 2013-09-16T14:30:48.137 回答
2

您所要做的就是将此功能添加到您的项目中......站起来con the 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# %20命令行%20界面

于 2013-12-05T21:53:00.213 回答
2

我正在使用 Visual Studio 2015,我通过添加通知插件解决了这个问题。

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

在此处输入图像描述

于 2016-06-21T14:27:32.400 回答
0

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

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

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

于 2013-10-30T20:08:38.863 回答
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();
于 2017-03-08T14:29:47.903 回答
0

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

于 2017-07-12T13:50:04.680 回答