1
votes

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;
    }
}
2

2 Answers

0
votes

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] ;
        }
    }
0
votes

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 ?