Ionic v1 Local Notifications Trigger Status

$rootScope.$on('$cordovaLocalNotification:trigger', function(event, notification, state) {
    //code
});

I've tried above code, but its just for when the app is already open while notification. I want to count all the notifications which are triggered while the app wasn't opened. Is there any way?

Upvotes: 1

Views: 341

Answers (2)

I've found a solution in this way that, I've these notifications in my local storage, on device ready I compare current time with scheduled times and calculate all the notifications that're triggered and that're not.

Upvotes: 0

Blauharley
Blauharley

Reputation: 4256

  1. When an app is closed (not just put into background with android for instance) and
  2. a notification is trigged or shown, then

the app-code can not run to keep a counter up to date stored in localStorage or a Sqlite-DB (for instance).

So the only way to increase a counter so that your request is fulfilled:

I want to count all the notifications which are triggered while the app wasn't opened

You have to use:

$rootScope.$on('$cordovaLocalNotification:click', function(event, notification, state) {
    // increase counter in your localStorage or Sqlite-DB here
});

because as this doc states:

The click event will also be called after deviceready if the app wasn’t running.

The click event will always be called when a notification is clicked, no matter if your app is open or not.


If you want to count only notifications that were clicked

  • While your app was not open OR
  • Your app is started, but an user is outside your app (relevant on iOS because app-code does not run when it's put into background or closed).

then you can do it in this way:

$ionicPlatform.ready(function() {
// ....
    $rootScope.$on('$cordovaLocalNotification:click', function(event, notification, state) {
        if(state === "background"){
           console.log("increase counter");
        }
    });
    // or if the first does not fire like with me, then use this instead:
    cordova.plugins.notification.local.on('click', function (notification, state) {
        if(state === "background"){
           console.log("increase counter");
        }
    });
// ....
});

Hope this helps as well.

Upvotes: 0

Related Questions