0

I have installed cordova (version 3.3) and built a sample project using these commands:

$ cordova create hello com.example.hello "HelloWorld"
$ cd hello
$ cordova platform add android
$ cordova build

and imported the project into Eclipse (according to http://cordova.apache.org/docs/en/3.3.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide). I am able to successfully run the app from Eclipse by selecting Run As → Android Application.

Now I want to make use of cordova's notification ablities. I added the plugins (following this guide: http://cordova.apache.org/docs/en/3.3.0/cordova_notification_notification.md.html#Notification) with the commands:

$ cordova plugin add org.apache.cordova.dialogs
$ cordova plugin add org.apache.cordova.vibration

and when I type:

$ cordova plugin ls

it correctly lists the plugins I just added.

I return to Eclipse and paste the following code into assets/www/index.html (overwriting the existing code in 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 device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        // Empty
    }

    // Show a custom alert
    //
    function showAlert() {
        navigator.notification.alert(
            'You are the winner!',  // message
            'Game Over',            // title
            'Done'                  // buttonName
        );
    }

    // Beep three times
    //
    function playBeep() {
        navigator.notification.beep(3);
    }

    // Vibrate for 2 seconds
    //
    function vibrate() {
        navigator.notification.vibrate(2000);
    }

    </script>
  </head>
  <body>
    <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
    <p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
    <p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
  </body>
</html>

When I deploy it to a device, it displays three links (Show Alert, Play Beep and Vibrate). When I press on these I expect the corresponding native notification to occur, but it doesn't. Instead I get the following error messages (displayed in LogCat):

Show alert: Uncaught ReferenceError: showAlert is not defined:45

Play Beep: Uncaught ReferenceError: playBeep is not defined:46

Vibrate: Uncaught ReferenceError: vibrate is not defined:47

How am I supposed to fix these errors?

Thanks in advance!

2 Answers2

0

In your question you didn't mention updating the config.xml and AndroidManifest.xml files as noted in the API documentation. I'll copy them here for reference.

(in app/res/xml/config.xml)
<feature name="Notification">
    <param name="android-package" value="org.apache.cordova.dialogs.Notification" />
</feature>
<feature name="Vibration">
    <param name="android-package" value="org.apache.cordova.vibration.Vibration" />
</feature>


(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.VIBRATE" />

For further clarification see the answer to this question. Should a phonegap plugin be declared in the config.xml file?

Community
  • 1
  • 1
Eric Carlson
  • 681
  • 1
  • 6
  • 11
  • Thanks for your reply! I did not mention updating the files as I noticed that the app/res/xml/config.xml and app/AndroidManifest.xml were already automatically updated when the plugins were added. – LittleGoldFish Jan 28 '14 at 08:38
0

Have you tried to update the files and run everything from the www folder instead?

gelly
  • 152
  • 1
  • 2
  • 10