How to Cancel Usernotifications

How to cancel UserNotifications

For cancelling all pending notifications, you can use this:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

For cancelling specific notifications,

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
var identifiers: [String] = []
for notification:UNNotificationRequest in notificationRequests {
if notification.identifier == "identifierCancel" {
identifiers.append(notification.identifier)
}
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}

How to cancel a local notification trigger in Swift

You can cancel or remove notifications by calling:

let center = UNUserNotificationCenter.current()

Remove pending notifications with given identifier

center.removePendingNotificationRequests(withIdentifiers: [“givenIdentifier”])

And remove delivered notifications with given identifier

center.removeDeliveredNotifications(withIdentifiers: [“givenIdentifier”])

Cancel UNNotificationRequest

You can also use the removeDeliveredNotifications(withIdentifiers:) function to remove already delivered notifications from the notification center. For more info, see the documentation

Cancel and reschedule UserNotification(old UILocalNotification) starting from custom date in iOS 10

After researching, this is not possible with the new UserNotifications Framework. We must use the old UILocalNotification approach to achieve this. Because that code is deprecated in iOS10, I've changed the supported version to be from 9+, so there will be no warnings. This is a permanent approach, but lets hope that Apple will implement and extend the old feature in the new framework.

how to cancel a localNotification with the press of a button in swift?

You could try to remove all notifications if this is acceptable in your context.
Like this:

for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] { 
UIApplication.sharedApplication().cancelLocalNotification(notification)
}

Or as stated by Logan:

UIApplication.sharedApplication().cancelAllLocalNotifications()

Or as stated by Gerard Grundy for Swift 4:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

Delete a particular local notification

You can save a unique value for key in your local notification's userinfo.
Get all local notification, loop through the array and delete the particular notification.

Code as follows,

OBJ-C:

UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
if ([uid isEqualToString:uidtodelete])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}

SWIFT:

var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications {
var notification = oneEvent as UILocalNotification
let userInfoCurrent = notification.userInfo! as [String:AnyObject]
let uid = userInfoCurrent["uid"]! as String
if uid == uidtodelete {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}

UserNotification:

If you use UserNotification (iOS 10+), just follow this steps:

  1. When creating the UserNotification content, add an unique identifier

  2. Remove specific pending notification using removePendingNotificationRequests(withIdentifiers:)

  3. Remove specific delivered notification using removeDeliveredNotifications(withIdentifiers:)

For more info, UNUserNotificationCenter



Related Topics



Leave a reply



Submit