1

I need to clear already displayed local notification from Notification Centre programmatically. Following this link I implemented suggested solution, but the problem is that eventArray from this example always has 0 elements. When I swipe down to display Notification Centre I see 4 notifications that I've created previously. So in this case I expect this array to have 4 elements, but it has 0. Any idea why is this so? I've tried on iOS 8.3 and 9.2.1 and array is 0 on both of them.

iOS has 2 ways of presenting local notifications:

  1. From Notification Center:
    • You can't swipe away notification from left to right.
    • You can swipe notification from right to left (deleting single notification from the list).
    • You can click on the notification, after which your app will start and notification will be removed from Notification Center (handled by iOS system)
  2. From Lock screen:
    • Available only if you enable this setting from iPhone/iPad settings: http://www.imore.com/how-disable-notification-center-lock-screen-your-iphone-and-ipad
    • You can swipe notification from left to right (your app will be started, handled by iOS). In this case notification from Notification Center is not deleted (iOS doesn't delete it, and doesn't allow deleting a single notification from code after notification is already presented to the user in Notification Center).
    • You can swipe notification from right to left (deleting single notification from the list, handled by iOS. Notification Center notification is also deleted, handled by iOS).
    • You can't click on the notification.

EDIT: Here is code sample how I did it:

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"1";
localNotification.alertTitle = @"1";
localNotification.userInfo = uniqueDictIdentifier1;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
localNotification2.alertBody = @"2";
localNotification2.alertTitle = @"2";
localNotification2.userInfo = uniqueDictIdentifier2;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification2];

....
//2 more notifications are created like this

And then there is code for filtering all notifications:

NSArray *eventArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++) {
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    if ([userInfoCurrent isEqualToDictionary:uniqueDictIdentifier1]) {
        [[UIApplication sharedApplication] cancelLocalNotification:oneEvent];
        break;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

For saving a notification for a unique id

 NSDictionary *  infoDict = @{ @"alarmUiqueId" : uID,

                                  };
    NSLog(@"%@",infoDict);
    NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitSecond
                                                             fromDate:fireDate];
    fireDate = [fireDate dateByAddingTimeInterval:-comp.second];
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = fireDate;
    localNotif.timeZone = [NSTimeZone localTimeZone];
    localNotif.alertBody = desString;
    localNotif.userInfo = infoDict;
    localNotif.repeatInterval = NSCalendarUnitDay;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]

and for delete a paritcular notification write this code.

  NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];

    for(UILocalNotification *notification in notificationArray)
    {
        NSLog(@"%@",[notification.userInfo valueForKey:@"alarmUiqueId"]);
        if ([[notification.userInfo valueForKey:@"alarmUiqueId"] isEqualToNumber: health.uniqueId])
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notification] ;
        }
    }
  • I've edited my question to explain further more what the problem is. Bolded text explains in which situation I need to delete a single notification. Linked stackoverflow issue has the code for removing a single notification. – Aleksandar Lugonja Apr 15 '16 at 08:12
  • you want to delete a particular notification?? – Muhammad Salman Apr 15 '16 at 09:27
  • Yes, I want to delete a single notification that is already displayed in the Notification Center. I just downloaded Gmail app and confirmed that it has the same problem as my app. So I guess this is more an iOS limitation, than a bug in my app. – Aleksandar Lugonja Apr 15 '16 at 09:45
  • You have to pass a unique id when saving the notification and than in delete function you have to get the array of uinotification and check if the notification.userinfo is equal to "unique id" than cancel the notification. – Muhammad Salman Apr 15 '16 at 10:07
  • I am doing that, but in case you did not read the question, my problem is that `notificationArray` contains 0 elements. And I can see that 4 local notifications are presented on the screen. – Aleksandar Lugonja Apr 19 '16 at 06:43
  • I edited my question, and provided the code. Like I said, `eventArray` here is of size 0. – Aleksandar Lugonja Apr 19 '16 at 14:45
0

My understanding is below: You can not get the information of already notified LocalNotification with scheduledNotifications.

My resolution is that just keep UILocalNotification instance in your singleton object in your application, and call cancelLocalNotification with the instance when you want to delete from Notification Center.

Is this could be your help ?

M.Masa
  • 532
  • 4
  • 20