9
votes

I wanted to first say this is a really nice plugin (https://github.com/katzer/cordova-plugin-local-notifications) but having some difficulties getting it working.

I am using an Android and Phonegap CLI. I have tried both CLI 5.0 and now Phonegap 3.5.0, this is my config.xml:

<preference name="phonegap-version" value="3.5.0" />

In my config.xml I have tried all these combinations:

<plugin name="de.appplant.cordova.plugin.local-notification"  spec="0.8.1" source="pgb" />
<gap:plugin name="de.appplant.cordova.plugin.local-notification" />
<plugin name="de.appplant.cordova.plugin.local-notification" source="pgb" />

However the notifications do not appear - nothing happens on the phone - nothing, nada, zilch. I have also downloaded the KitchenSink App (https://github.com/katzer/cordova-plugin-local-notifications/tree/example) and installed on Phonegap build and my phone and nothing again happens..

This is my code on index.html so when the phone fires it should register a local notification asap:

cordova.plugins.notification.local.registerPermission(function (granted) {
    // console.log('Permission has been granted: ' + granted);
});

cordova.plugins.notification.local.schedule({
    id: 1,
    title: 'Reminder',
    text: 'Dont forget to pray today.',
    every: 'minute',
    icon: 'res://icon',
    smallIcon: 'res://ic_popup_sync'
});

I also tried

cordova.plugins.notification.local.schedule({
    id: 2,
    text: "Good morning!",
    firstAt: tomorrow_at_8_am,
    every: "day" // "minute", "hour", "week", "month", "year"
});

Even the KitchenSink app is not working - nothing happens on the phone??

My Android version is: 5.1.1

How can I get local notifications to appear in Phonegap?

2
Have you verified that the deviceready event has fired?chadiusvt
Yes, the app responds with this. I have done console.log and various other tests both in Phonegap build and making a .apk on the phone - the kitchen sink app is not working eitherTheBlackBenzKid
@TheBlackBenzKid Hi, i m just trying out with the kitchensink app. Will let you know by tomorrow.But one thing i noticed while downloading the sample code is that the plugin folder is not getting extracted properly. I m not able to find 'de.appplant.cordova.plugin.local-notification' folder inside the plugins. I could see only 'de.appplant.cordova.plugin.local-notification' file of size 1kb. So could you confirm that your plugin is installed properly by checking the same in your plugins folder?Gandhi
@TheBlackBenzKid Any update on this? Have you managed to make it work?Gandhi
How are you trying? Notifications should only appear when the app is in background, and probably you are testing in foreground.Víctor

2 Answers

3
votes

I too have spent many hours trying to get this plugin working & I have, but i do find it to be one of the most temperamental.

Within your js -

var testNotifications = function () {

document.addEventListener("deviceready", function () {

  console.warn("testNotifications Started");

  // Checks for permission
  cordova.plugin.notification.local.hasPermission(function (granted) {

    console.warn("Testing permission");

    if( granted == false ) {

      console.warn("No permission");
      // If app doesnt have permission request it
      cordova.plugin.notification.local.registerPermission(function (granted) {

        console.warn("Ask for permission");
        if( granted == true ) {

          console.warn("Permission accepted");
          // If app is given permission try again
          testNotifications();

        } else {
          alert("We need permission to show you notifications");
        }

      });
    } else {

      var pathArray = window.location.pathname.split( "/www/" ),
          secondLevelLocation = window.location.protocol +"//"+ pathArray[0],
          now = new Date();


      console.warn("sending notification");

      var isAndroid = false;

      if ( device.platform === "Android" ) {
        isAndroid = true;
      }

      cordova.plugin.notification.local.schedule({
          id: 9,
          title: "Test notification 9",
          text: "This is a test notification",
          sound: isAndroid ? "file://sounds/notification.mp3" : "file://sounds/notification.caf",
          at: new Date( new Date().getTime() + 10 )
          // data: { secret:key }
      });

    }

  });

  }, false);

};

Now on your html tag -

<button onclick="testNotifications()">Test notification</button>

That should trigger a notification or warn you that it needs permissions Also top tip is to make sure your notifications are in a folder in the root of the project. android should be mp3 and ios caf

1
votes

Answer 1 :for version 3.5.0

have a look at plugin's plugin.xml. see line 22

    <engine name="cordova" version=">=3.6.0" />

that means plugin only supports version greater than 3.6.0 and you are using 3.5.0

Answer 2 :for version 5.0 or higher

Try the following code as index.html. if it runs perfectly then and other options in to notification.schedule. as we haven't provided time(at) option notification will triggered immediately.

<html>
<script type="text/javascript" src="cordova.js"></script>
<script>
document.addEventListener('deviceready', onDeviceReady.bind(this), false);
        function onDeviceReady() {                               
            cordova.plugins.notification.local.schedule({
                id: 1,
                title: "Sample Notification",
                text: "foo",                    
                every: "week",                                        
                data: { meetingId: "123#fg8" }
            });
        };
</script>
<body>
</body>
</html>