PhoneGap通知警报不起作用。

3

好的,我已经一直在解决这个问题了,但仍然无法弄清楚。这是一个简单的PhoneGap测试应用程序,试图显示警报。

使用Cordova 2.9.0 for iOS。我添加了一些简单的测试代码,并在Chrome中进行了测试以查看它在哪里出错,因为它在模拟器中不起作用。

当我在Chrome中进行测试时(当然,在模拟器中结果相同,但没有错误信息显示)

  • 它执行onDeviceReady的操作
  • 它将tb2文本框值设置为“before alert”
  • 然后它在此行上发生错误:navigator.notification.alert(...,错误消息为Uncaught TypeError: Cannot call method 'alert' of undefined。

它应该正确引用cordova.js,这是我的应用程序文件夹的结构:

  • cordova_plugins.js
  • cordova.js
  • /spec
  • spec.html
  • config.xml
  • /css
  • home.html
  • /img
  • index.html
  • /js
  • /res

这是我的config.xml代码:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.blahblahblah.hello" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Hello World</name>
    <description>
        Test blahblahblah Application
    </description>
    <author email="blahblahblah@blahblahblah.com" href="http://blahblahblah.com">
        blahblahblah
    </author>
    <access origin="*" />

    <preference name="fullscreen" value="true" />
    <preference name="webviewbounce" value="true" />

    <plugins>
        <plugin name="Notification" value="CDVNotification" />
    </plugins>
</widget>

这是我的index.html代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Notification Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Cordova is ready
    //
    function onDeviceReady() {
        // Empty
        document.getElementById('tb1').value = 'device ready';
    }

    // alert dialog dismissed
    function alertDismissed() {
        // do something
    }

    // Show a custom alert
    //
    function showAlert() {
        document.getElementById('tb2').value = 'before alert';

        navigator.notification.alert(
            'You are the winner!',  // message
            alertDismissed,         // callback
            'Game Over',            // title
            'Done'                  // buttonName
        );
        document.getElementById('tb3').value = 'after alert';
    }

    </script>
  </head>
  <body>
    <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
    <input type="text" id="tb1" value="" />
    <input type="text" id="tb2" value="" />
    <input type="text" id="tb3" value="" />
  </body>
</html>

我已经查阅了文档,但没有找到为什么这不起作用的线索,大多数对这个问题的回答都没有涉及2.9.0版本。

提前感谢。


我遇到了相同的问题,搜索了一下,发现这个链接: iOS通知警报无法工作-Phonegap 2.3.0 - Milchkanne
4个回答

10

我知道这个问题是关于Phonegap 2.9的,但当有人搜索"phonegap alert not working"时,这是谷歌第一个推荐的结果。所以以下是我在Phonegap 3.0中使其工作的方法:

根据手册,您需要将插件添加到您的项目中。只需导航到您的项目根目录并编写以下命令:

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-dialogs.git

之后,我将以下内容添加到我的html中:

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script>
    document.addEventListener("deviceready", onDeviceReady, true);
    function onDeviceReady() {

        navigator.notification.alert("PhoneGap is working", function(){}, "", "");
    } 
</script> 

@Andre 我尝试了上面的答案,在我的项目根目录中运行上述命令,但是我收到了以下错误“-bash:phonegap:找不到命令”。有任何想法是什么问题。 - user2086641
@user2086641,你可能没有安装PhoneGap或者它不在你的路径中。请查看网站获取安装指南。 - Andre
cordova插件添加org.apache.cordova.dialogs - 这也会添加对话框插件。 - P-RAD

0

你所需要做的就是添加插件:

cordova-plugin-dialogs

然后使用中断程序流的警报功能:

alert("some problem here");

适用于iOS和Android。


0

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

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

希望这有所帮助...


0

我正在使用Phonegap 2.9.0,遇到的问题是我没有将脚本cordova.js添加到页面中。

还要注意的是,每个平台都有一个特定的cordova.js文件,因此请注意不要在iOS上添加来自Android的cordova.js。

请记住,在deviceready触发后才能调用所有对Phonegap API的调用。


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