PhoneGap Notification.Alert不工作 [英] PhoneGap Notification.Alert not working

查看:350
本文介绍了PhoneGap Notification.Alert不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在这个问题上工作了一段时间,不能算出这个东西。 Simple PhoneGap测试应用程式,尝试显示快讯。



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



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




  • 它执行onDeviceReady,因为它应该

  • 它将tb2文本框的值设置为'before alert'

  • 然后它断开与错误:未捕获TypeError:不能调用未定义的方法'alert',在此行:navigator。 notification.alert(...



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


  • cordova_plugins.js

  • cordova.js

  • spec


  • / css

  • home.html

  • / img

  • index.html

  • / js

  • / res



这是我的config.xml代码:

 <?xml version ='1.0'encoding ='utf-8'? 
< widget id =com.blahblahblah.helloversion =0.0.1xmlns =http://www.w3.org/ns/widgetsxmlns:cdv =http:// cordova .apache.org / ns / 1.0>
< name> Hello World< / name>
< description>
Test blahblahblah应用程序
< / description>
< author email =blahblahblah@blahblahblah.comhref =http://blahblahblah.com>
blahblahblah
< / author>
< access origin =*/>

< preference name =fullscreenvalue =true/>
< preference name =webviewbouncevalue =true/>

< plugins>
< plugin name =Notificationvalue =CDVNotification/>
< / plugins>
< / widget>

这是我的index.html代码:

 <!DOCTYPE html> 
< html>
< head>
< title>通知示例< / title>

< script type =text / javascriptcharset =utf-8src =cordova.js>< / script&
< script type =text / javascriptcharset =utf-8>

//等待Cordova加载
//
document.addEventListener(deviceready,onDeviceReady,false);

// Cordova准备就绪
//
function onDeviceReady(){
//空的
document.getElementById('tb1')。value = '设备就绪';
}

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

显示自定义警报
//
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;>显示警报< / a>< / p&
< input type =textid =tb1value =/>
< input type =textid =tb2value =/>
< input type =textid =tb3value =/>
< / body>
< / html>

我搜索过文档,没有找到任何线索,

解决方案

/ div>

我知道这个问题是关于Phonegap 2.9,但这是谷歌第一件事,当有人寻找phonegap警报不工作。所以这里是我为它使用Phonegap 3.0工作:



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



$ phonegap本地插件添加https://git-wip-us.apache。 org / repos / asf / cordova-plugin-dialogs.git



之后,我将它添加到我的html:

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

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


Okay I've been working on this issue for a while now and can't figure this thing out. Simple PhoneGap test app, trying to show an alert.

Using Cordova 2.9.0 for iOS. I've added some simple test code and tested it in chrome to see where it breaks, because it isn't working in the emulator

When I test in the Chrome (of course same result in emulator, but no error message is showing)

  • It executes the onDeviceReady as it should
  • It sets tb2 textbox value to 'before alert'
  • Then it breaks with the error: Uncaught TypeError: Cannot call method 'alert' of undefined, on this line: navigator.notification.alert(...

It should be referencing the cordova.js properly, here is the structure of my app folder:

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

Here is my config.xml code:

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

Here is my index.html code:

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

I have searched documentation, and haven't found any clue of why this isn't working, most answers to this question don't address version 2.9.0

Thanks in advance.

解决方案

I know the question is about Phonegap 2.9, but that's the first thing Google spits when somebody looks for "phonegap alert not working". So here's what I did for it to work with Phonegap 3.0:

According to the manual, you need to add the plugin to your project. Just navigate to your project root folder and write this command:

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

After that, I added this to my 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> 

这篇关于PhoneGap Notification.Alert不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆