3

I'm using a UILocalNotification object to give notification to my application. Currently each time an event is generated i popup a notification.

  notification.alertBody=@"Event Occurs 2";
  notification.alertAction=@"Open";
  [[UIApplication sharedApplication]presentLocalNotificationNow:notification];

But, since the events keep on happening, each time a new notification is generated.

Is there a way to update a notification, if it is already present and create a new notification if not present.

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
slaveCoder
  • 519
  • 2
  • 17
  • 46

3 Answers3

4

You can't update an already scheduled Local Notication. You can however, cancel it and reschedule a new one.

Cancel your local notification:

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 the specific local notification
        [app cancelLocalNotification:oneEvent];
        //Schedule your new "updated" local notification here.
        break;
    }
}

This will loop through all scheduled local notifications and delete the local notification you want deleted. Note, you'll need to set a unique property to each notification to distinguish between others (in the example above, it is assumed userInfo contains a unique "uid").

Thanks to KingofBliss for code above on how to delete specific local notifications.

Community
  • 1
  • 1
joels
  • 1,292
  • 15
  • 21
  • Is this documented somewhere? Because push notifications can be updated. – nr5 Jul 12 '18 at 13:58
  • I can't find any documentation regarding updating an already scheduled notification. Note that UILocalNotification has been deprecated and you should now use UNNotificationRequest instead. Best place to start is https://developer.apple.com/documentation/usernotifications/unusernotificationcenter – joels Jul 12 '18 at 15:08
  • I just tried UNNotificationRequest and sent a notification with the same identifier. It got updated. Although I see multiple alerts but the notification bubble is same. – nr5 Jul 13 '18 at 07:01
1

It's not possible to update the notification but if you attach a dictionary with a key to the alert:

UILocalNotification *notification = [[UILocalNotification alloc] init];

NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:alarmID forKey:@"AlarmKey"];
// Set some extra info to your alarm
notification.userInfo = userInfo;

Then you can retrieve the local notification, cancel it and make a new one with updated content.

+ (UILocalNotification *)existingNotificationWithAlarmID:(NSString *)alarmID
{
    for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if ([[notification.userInfo objectForKey:@"AlarmKey"] isEqualToString:alarmID]) {
            return notification;
        }
    }

    return nil;
}

You cancel the notification like this:

- (void)cleanUpLocalNotificationWithAlarmID:(NSString *)alarmID
{
    UILocalNotification *notification = [self existingNotificationWithAlarmID:alarmID];
    if (notification) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}
3lvis
  • 4,190
  • 1
  • 30
  • 35
0

No, there is no way to modify a local notification that has been scheduled. You'll have to cancel the notification and schedule it again.

Matías R
  • 2,195
  • 1
  • 14
  • 12