Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
TheBlackBenzKid Avatar asked Mar 30 '16 14:03

TheBlackBenzKid


2 Answers

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

like image 180
Mark Avatar answered Sep 19 '22 00:09

Mark


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>
like image 26
Arpit Vasani Avatar answered Sep 20 '22 00:09

Arpit Vasani