Search code examples
iosobjective-cuilocalnotificationunusernotificationcenterunnotificationrequest

All delivered Scheduled Notifications are cleared from Notification Center when one is tapped


I scheduled multiple Local Notification for the user. All of them are also delivered on their specified time. However, when I try to open any one of them from Notification Center, all of them are getting cleared.

In a rightful scenario, I don't want all them to be cleared from notification centre, only those which are tapped to be opened.

Also, i tried commenting below code from AppDelegate.m, but the issue still persists. [[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];

Can anyone tell me what could be the issue here due to which my scheduled notifications are cleared from Notification Center even when I'm tapping to open only one of them?

Below is the code I'm using to schedule Local Notifications -

NSDateComponents *components = [SSUtility hoursMinuteAndSectionsForDate:date];

    NSInteger hour = [components hour];
    NSInteger minute = [components minute];

    NSLog(@"Hour %ld Min %ld ", hour,minute);

    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

    /* Set notification */

    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.body = body;
    // content.categoryIdentifier=NSNotificationC;
    content.sound = [UNNotificationSound defaultSound];
    content.userInfo = userInfo;
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                          content:content
                                                                          trigger:trigger];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (error != nil) {
            SSLOG(@"Something went wrong: %@",error);
        }
    }];

Solution

  • So i did't believe that this is a default behaviour so what i found:

    • if you use UNCalendarNotificationTrigger then all delivered reminders are deleted on tap
    • if you use UNTimeIntervalNotificationTrigger then delivered reminders remain on the notification center

    so try to use the UNTimeIntervalNotificationTrigger instead of UNCalendarNotificationTrigger

    NSTimeInterval timeInterval = [fireDate timeIntervalSinceDate:[NSDate date]];
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];