Cancel local notification ngCordova

I am using the local ngCordova notification.

I’m trying to cancel a notification but I’m not succeeding

$ionicPlatform.ready(function() {

		$cordovaLocalNotification.schedule({
            id: 1,
            title: 'Warning',
            text: 'Test!',
		    at: _10SecondsFromNow,
            data: {
              customProperty: 'custom value'
            }
          }).then(function (result) {
            console.log('Notification 1 triggered');
          });

			$cordovaLocalNotification.cancelAll().then(function (result) {
			console.log(result);
			});

	})

The $cordovaLocalNotification.cancelAll () method is completely ignored

Are you facing this is iOS 10?

@thesourav
Android 6.0.1 Marshmallow

You can put a debugger on

 $rootScope.$on('$cordovaLocalNotification:cancelall', function(event, state) {}) to check if it gets fired or not.

Secondly, i read somewhere that cancelallnotifications have been deprecated in IOS 10. Maybe thats why its not working( Not Sure ). I don’t see it in the cordova repo wiki also :

If you don’t find the solution, you can have a workaround by getting all IDs and then canceling them in a loop.

window.plugin.notification.local.getScheduledIDs(function(localIDlist){
 for(var i = 0;localIDlist.length > i; i++){
  window.plugin.notification.local.cancel(localIDlist[i]);
 }
})

I’ll try this, thank you again

@thesourav

I tried this:

$ionicPlatform.ready(function() {
  cordova.plugins.notification.local.schedule({
  id         : 1,
  title      : 'I will bother you every minute',
  text       : '.. until you cancel all notifications',
  sound      : null,
  autoClear  : false,
  at         : _10SecondsFromNow
});

cordova.plugins.notification.local.getScheduledIds(
 function (scheduledIds) {
    for (var i = 0; scheduledIds.length > i; i++) {
	window.plugin.notification.local.cancel(localIDlist[i]);
	}
    }
  )
}) 

But without success, I changed the plugin to this one because it seems more updated:

I did it this way too:

window.plugin.notification.local.getScheduledIDs.getScheduledIds(
  function (scheduledIds) {
	for (var i = 0; scheduledIds.length > i; i++) {
	window.plugin.notification.local.cancel(localIDlist[i]);
   }
 }
)

Does not have support for the version of android 6?
Thanks

So, were you successful?

@thesourav

Sorry for not being so clear. I did not succeed :frowning:

I think I’ll look for another way, because canceling notifications does not seem to be very good. The onesignal for example did not even implement this method

Just to be clear, What do you plan to achieve using cancel notification?
What should happen

@thesourav

The idea is this:

The user enters a parking lot, when he clicks on pay I would schedule a notification for when missing a few minutes to the end of the contracted time I display a notification to renew. But the condition for this is if it is at least 1km from the parking lot. If it is not in this range, I would cancel the notification.

Thanks a lot for the help

Nice use case.
Have you looked into OneSignal’s clearOneSignalNotifications() and cancelNotification() methods, which can be used if you need to programmatically dismiss notifications so they are not restored.

I’ve looked at cancelNotification, but it has not yet been implemented by the oneSignal team
.
cancelNotification
METHOD - COMING SOON

The ClearOneSignalNotifications I did not know, in the documentation of the ionic Sdk in the onesignal it is not present.

I found it in the Unity SDK and android native SDK.

Can you tell me if he has ionic / cordva support?

i think the API does that. You have to send a DELETE request and pass your id in the url.
https://documentation.onesignal.com/reference#cancel-notification

1 Like

Thank you for your help. Helped me a lot.

I’ll try this

Sure, let me know if you were successful

1 Like

@thesourav

That worked!! Thank you very much, I never imagined that possibility

I used this:

https://onesignal.com/api/v1/notifications/{notificationId}?app_id={appId}

Perfect. I have switched to OneSignal because its awesome. Yet to try out the cancel notification functionality. Cheers

1 Like

Sounds like you got this solved a different way, but I thought I’d mention the original problem may well have been that you deleted before the notification was registered.

It takes time to schedule a notification, hence it returning a promise. Rather than waiting for it to come back successfully registerd, you call cancelAll immediately.

1 Like

@rlouie

At the beginning of all my idea was to do with the onesignal, so much that I already had the code ready. But as in the documentation the cancel function was like “soon” I tried $cordovaLocalNotification as an alternative.

But the idea was always to use the onesignal, with the help of @thesourav I was able to execute the main plan.

I think you’re right about promise, as I just remembered that in one of the tests the alert of the cancel method came first.

Well, anyway, that’s all right now.

Thank you all!