I have a problem with my UILocalNotification.
I am scheduling the notification with my method.
- (void) sendNewNoteLocalReminder:(NSDate *)date a
In swift, you first add a dict to notifications userInfo by:
let dict:NSDictionary = ["ID" : "someString"]
notification.userInfo = dict as! [String : String]
Then, you want to get an array of existing notifications (2), and iterate through each one (3) until you find the one you're looking for. Once you find it, cancel it (4).
// 1. Pass in the string you want to cancel
func cancelLocalNotification(uniqueId: String){
// 2. Create an array of notifications, ensuring to use `if let` so it fails gracefully
if let notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications {
// 3. For each notification in the array ...
for notif in notifyArray as [UILocalNotification] {
// ... try to cast the notification to the dictionary object
if let info = notif.userInfo as? [String: String] {
// 4. If the dictionary object ID is equal to the string you passed in ...
if info["ID"] == uniqueId {
// ... cancel the current notification
UIApplication.sharedApplication().cancelLocalNotification(notif)
}
}
}
}
}
Swift version:
UIApplication.sharedApplication().cancelAllLocalNotifications()
Thank @viggio24 for the good answer, this is a swift version
var notifyCancel = UILocalNotification()
var notifyArray=UIApplication.sharedApplication().scheduledLocalNotifications
var i = 0
while(i<notifyArray.count){
if let notify = notifyArray[i] as? UILocalNotification{
if let info = notify.userInfo as? Dictionary<String,String> {
if let s = info["name"] {
if(name==s){//name is the the one you want cancel
notifyCancel = notify
break
}
}
}
}
i++
}
My solution is to remove all scheduled Notifications on these methods.
-applicationDidFinishLaunchingWithOptions:
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
with
[[UIApplication sharedApplication] cancelAllLocalNotifications];
You can still get to the LocalNotification object that activated your app by using.
UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
Then I reschedule new Notifications on
- (void)applicationDidEnterBackground:(UIApplication *)application;
This avoids to have bookkeeping for all the notifications. I send some context information in the userInfo dictionary as well. This then helps to jump to the right place in the app.
Swift 5:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)
My solution is you when you create UILocalNotification
at that time you create one NSMutableDictionary
and store that notification as a value for key as your ID and put this NSMutableDictionay
to your NSUserDefaults
So when you want to cancel any particular localnotification at that time you write
[dictionary valueforkey @"KEY"]
where as a key you pass your id so you get that particular local notification and pass it to
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];