Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

ios - PhoneGap Notification.Alert not working

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...