Removing UILocalNotification from notification tray programmatically

后端 未结 7 1761
再見小時候
再見小時候 2020-12-29 10:29

Is there a way to programmatically remove/dismiss UILocalNotification from Notification Tray. I am able to cancel the notification which removes the notificatio

相关标签:
7条回答
  • 2020-12-29 10:34

    I believe I had a similar issue. When the app entered the foreground I attempted to clear past notifications to remove any old notifications from the notifications tray.

    I did something like this to grab old notifications and remove them:

    NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    NSArray *pastNotifications = [activeNotifications filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"firDate < %@", [NSDate date]]];
    for (UILocalNotification *notification in pastNotifications) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
    

    However, it seems that scheduledLocalNotifications does not include locations whose fire date is already past even though they still appear in notification center.

    Calling cancelAllLocalNotifications does seem to remove past notifications as well. So we can grab all the current notifications, cancel everything, and then add the ones we're still interested in back.

    // Grab all the current notifications
    NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    // Clear all notifications to remove old notifications
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    // Add back the still relevant notifications
    for (UILocalNotification *notification in activeNotifications) {
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    

    Additionally we can do some filtering of the notifications before adding them back if some are no longer needed, and we can grab the active notifications when the app becomes active, store them in an instance variable, and only add them back when the app moves to the background

    0 讨论(0)
  • 2020-12-29 10:48
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    

    will do some trick too

    but if you didnot use applicationIconBadgeNumber, it will not work, so trick is set applicationIconBadgeNumber first :)

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    
    0 讨论(0)
  • 2020-12-29 10:48

    I was fiddling with some code and I was wondering why local notifications are stored in the notification center if the application is in the foreground. It's probably because Apple doesn't know what you are doing with them and honestly doesn't care; so they do their job.

    As far as the question is concerned, I do the following:

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
        if (application.applicationState == UIApplicationStateActive)
        {
            NSLog(@"active");
    
            // display some foreground notification;
            [application cancelLocalNotification:notification];
        }
        else
        {
            NSLog(@"inactive");
        }
    }
    
    0 讨论(0)
  • 2020-12-29 10:48

    So I just read this thread about how to close/remove all the already fired local notifications from the Notification center, if the user opens the app by clicking the app icon, not the notification. But after all of this, the other scheduled local notification should fire in the future.

    Here is my easy solution for this, which should be triggered on application did becomeActive:

    UIApplication* application = [UIApplication sharedApplication];
    NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
    application.scheduledLocalNotifications = scheduledNotifications;
    

    I ve tried the [[UIApplication sharedApplication] cancelLocalNotification:notification]; but it did not clear the already fired local notifications from the Notification center (outside of the app).

    0 讨论(0)
  • 2020-12-29 10:53

    If the Application is not running, you will be receiving the Local Notification object in the

    -applicationDidFinishLaunchingWithOptions:

    like:

    UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
    

    or else you can get it in

    • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

    Now you can remove it from the Notification Center using

    [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

    0 讨论(0)
  • 2020-12-29 10:54

    You can cancel all notifications using:

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

    If you want to remove a particular notification, you can use userinfo of notification object, when you create a local notification add a unique ID to that. Later you can use that ID for removing local notification.

    For that you can use the following code:

    NSString *notificationId = @"id_to_cancel";
    UILocalNotification *notification = nil;
    for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
    {
      if([[notify.userInfo objectForKey:@"ID"] isEqualToString:notificationId])
      {
         notification = notify;
         break;
      }
    }
    [[UIApplication sharedApplication] cancelLocalNotification:notification];
    
    0 讨论(0)
提交回复
热议问题