Search code examples
androidcordovaphonegap-pluginsphonegap-build

Cordova Phonegap local notifications not working


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?


Solution

  • 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