PhoneGap Notification.Alert不工作

时间:2013-07-17 06:10:10

标签: ios cordova phonegap-plugins

好的,我一直在研究这个问题已经有一段时间了,无法解决这个问题。简单的PhoneGap测试应用,试图显示警报。

使用适用于iOS的Cordova 2.9.0 。我添加了一些简单的测试代码并在chrome中测试它以查看它在哪里中断,因为它在模拟器中不起作用

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

  • 它应该执行onDeviceReady
  • 将tb2文本框值设置为“警告前”
  • 然后它突然出现错误:Uncaught TypeError:无法在此行调用未定义的方法'alert':navigator.notification.alert(...

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

  • cordova_plugins.js
  • cordova.js
  • /规范
  • 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

提前致谢。

4 个答案:

答案 0 :(得分:10)

我知道问题是关于Phonegap 2.9,但这是谷歌在有人寻找“电话间隙警报不工作”时首先吐出的东西。所以这就是我使用Phonegap 3.0所做的工作:

根据manual,您需要将插件添加到项目中。只需导航到项目根文件夹并编写以下命令:

$ 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> 

答案 1 :(得分:0)

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

另请注意,存在特定于每个平台的cordova.js文件,因此请注意在iOS上从android添加cordova.js。

请记住,所有对phonegap API的调用都应在 deviceready 解雇后完成

答案 2 :(得分:0)

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

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

..我希望有帮助...

答案 3 :(得分:0)

您只需要添加插件:

cordova-plugin-dialogs

然后使用会中断程序流程的alert函数:

alert("some problem here");

适用于iOS和Android。