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:
When creating the UserNotification content, add an unique identifier
Remove specific pending notification using removePendingNotificationRequests(withIdentifiers:)
Remove specific delivered notification using removeDeliveredNotifications(withIdentifiers:)
For more info, UNUserNotificationCenter
Related Topics
Multiple Uialertcontrollers in iOS
Safari Web View Opening When Logging to Fb Through iOS 9
How to Make a Conical Gradient in iOS Using Core Graphics/Quartz 2D
How to Add a Custom Separator to Uitableviewcell
Parsing JSON Using the New Swift 3 and Alamofire
Initializing Swift Properties That Require "Self" as an Argument
How to Create Multi Line Uisegmentedcontrol
Admob 7.3.1 and Swift 2.0 - Module Not Found
Avplayerviewcontroller Using Audio-Only Avplayer
Autolayout Ignores Multi-Line Detailtextlabel When Calculating Uitableviewcell Height (All Styles)
How to Capture Taps Ignored by Wkwebview
Stop Skaction That Repeatsforever - Sprite Kit
Googleutilities/Appdelegateswizzler/Private/Gulapplication.H' File > Not Found
How We Can Set the Light Content Style of Status Bar in iOS 9 for Whole Application
Uiwebview Stringbyevaluatingjavascriptfromstring Hangs on iOS5.0/5.1 When Called Using Gcd