以编程方式从通知托盘中删除UILocalNotification

有没有办法编程从通知托盘中删除/解除UILocalNotification 。 我可以取消通知,从中删除通知

 [[UIApplication sharedApplication] scheduledLocalNotifications] 

这是我需要做的

我需要在操作执行后(即在用户点击通知后)从NotificationTray中解除UILocalNotification

编辑:我可以从NSNotificationCenter删除通知。 我想从Notification Tray中删除特定的通知。就像用户按下清除button清除属于特定应用程序的所有通知一样。

您可以使用以下方式取消所有通知

 [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

如果你想删除一个特定的通知,你可以使用通知对象的userinfo ,当你创build一个本地通知时,添加一个唯一的ID。 稍后,您可以使用该ID来删除本地通知。

为此,您可以使用以下代码:

 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]; 

我相信我也有类似的问题。 当应用程序进入前台时,我试图清除过去的通知,以从通知托盘中删除任何旧的通知。

我做了这样的事情来抓取旧的通知,并删除它们:

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

然而,似乎scheduledLocalNotifications的本地通知不包括即使它们仍然出现在通知中心中,但它们的起火date已经过去的位置。

调用cancelAllLocalNotifications似乎也删除了过去的通知。 因此,我们可以抓取所有当前通知,取消所有内容,然后添加我们仍然感兴趣的内容。

 // 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]; } 

另外,如果某些通知不再需要时,我们可以对通知进行一些筛选,然后我们可以在应用程序变为活动状态时抓取活动通知,将它们存储在实例variables中,并且只在应用程序移至时才将其添加回来的背景

 [UIApplication sharedApplication].applicationIconBadgeNumber = 0; 

也会做一些伎俩

但如果你没有使用applicationIconBadgeNumber,它不会工作,所以技巧是设置applicationIconBadgeNumber第一:)

 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1]; 

如果应用程序没有运行,您将在接收本地通知对象

-applicationDidFinishLaunchingWithOptions:

喜欢:

 UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

否则你可以得到它

  • (void)应用程序:(UIApplication *)应用程序didReceiveLocalNotification:(UILocalNotification *)通知

现在可以使用通知中心将其删除

[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

 // deletes a pushnotification with userInfo[id] = id -(void)deleteLocalPushNotificationWithId:(NSString*)id{ for(UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]){ if([[notification.userInfo objectForKey:@"id"] isEqualToString:id]){ [[UIApplication sharedApplication] cancelLocalNotification:notification]; } } } // deletes all fired pushnotifications -(void)clearLocalPushNotifications{ 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]; } } 

我摆弄了一些代码,我在想为什么当应用程序在前台时,为什么本地通知存储在通知中心。 这可能是因为苹果不知道你在做什么,并且不在乎。 所以他们做他们的工作。

就这个问题而言,我做了以下工作:

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if (application.applicationState == UIApplicationStateActive) { NSLog(@"active"); // display some foreground notification; [application cancelLocalNotification:notification]; } else { NSLog(@"inactive"); } }