Reputation: 253
$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
Reputation: 253
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
Reputation: 4256
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
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