Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a particular local notification

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?

like image 933
Yogi Avatar asked Jun 14 '11 07:06

Yogi


People also ask

How do I cancel a notification?

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:

Why do I get permissions dialogs when I turn off notifications?

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.

How to initialize the local notification plugin with specific settings?

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).

How to use local notifications in flutter?

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


2 Answers

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

like image 155
KingofBliss Avatar answered Sep 27 '22 19:09

KingofBliss


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

like image 31
iMOBDEV Avatar answered Sep 27 '22 18:09

iMOBDEV