Removing UILocalNotification from notification tray programmatically

后端 未结 7 1794
再見小時候
再見小時候 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条回答
  •  旧时难觅i
    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];
    

提交回复
热议问题