I am developing an iPhone alarm app based on local notifications.
On deleting an alarm, the related local notification should get cancelled. But how can I determine exactly which object from the array of local notifications is to be cancelled?
I am aware of [[UIApplication sharedApplication] cancelLocalNotification:notification]
method but how can I get this 'notification' to cancel it?
When cancelling a notification you have two options: To cancel a specific notification, you have to use the notification id. To cancel all notifications, you use the cancelAll method:
These options are there, since the initialization of the local notifications plugin may cause the operating system to present permissions dialogs to the user at a time when you don’t want them to show up. If you don’t want this behavior, you can set all of these values to false.
We now need to initialize the local notification plugin with specific settings for Android and for iOS. To do so, we need to create an InitializationSettings object. It accepts arguments for the following operating systems: Android, iOS and MacOS. Here, it is fairly simple as there is only one mandatory argument to pass, defaultIcon (String).
Local Notifications In Flutter 1 Setup. To allow our application to use local notifications, we need to add the flutter_local_notifications package to our project. 2 Integration. ... 3 Use Cases - Showing A Notification. ... 4 Use Cases - Scheduling A Notification. ... 5 Canceling A 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
Other Option:
First of All, when you create local notification, you can store it in user defaults for future use, Local notification object can not be stored directly in user defaults, This object needs to be converted into NSData object first, and then NSData
can be stored into User defaults
. Below is code for that:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotif]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:@"%d",indexPath.row]];
After you have stored and scheduled local notification, In future, requirement may arise that you need to cancel any of notification that you created earlier, So you can retrieve it from User defaults.
NSData *data= [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%d",UniqueKey]]; UILocalNotification *localNotif = [NSKeyedUnarchiver unarchiveObjectWithData:data]; NSLog(@"Remove localnotification are %@", localNotif); [[UIApplication sharedApplication] cancelLocalNotification:localNotif]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%d",UniqueKey]];
Hope This helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With