12

有没有办法以编程方式UILocalNotification从通知托盘中删除/关闭。我可以取消从中删除通知的通知

[[UIApplication sharedApplication] scheduledLocalNotifications]

这是我需要做的

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

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

4

7 回答 7

16

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];
于 2013-01-03T19:07:54.930 回答
6

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

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

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

但是,似乎scheduledLocalNotifications不包括火灾日期已经过去的位置,即使它们仍然出现在通知中心。

通话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];
}

此外,如果不再需要某些通知,我们可以在添加它们之前对它们进行一些过滤,并且我们可以在应用程序变为活动时获取活动通知,将它们存储在实例变量中,并且仅在应用程序移动到时才将它们添加回来的背景

于 2014-01-13T16:00:15.053 回答
2
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

也会做一些伎俩

但是如果你没有使用 applicationIconBadgeNumber,它就不会起作用,所以技巧是先设置 applicationIconBadgeNumber :)

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
于 2013-01-03T16:06:40.873 回答
1
// 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];
  }
}
于 2014-10-04T14:09:17.187 回答
1

如果应用程序没有运行,您将在

-applicationDidFinishLaunchingWithOptions:

喜欢:

UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];

否则你可以把它放进去

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

现在您可以使用它从通知中心中删除它

[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

于 2013-01-03T16:28:15.740 回答
1

我正在摆弄一些代码,我想知道如果应用程序在前台,为什么本地通知会存储在通知中心。这可能是因为 Apple 不知道你在用它们做什么,而且老实说也不在乎;所以他们做他们的工作。

就问题而言,我执行以下操作:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if (application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"active");

        // display some foreground notification;
        [application cancelLocalNotification:notification];
    }
    else
    {
        NSLog(@"inactive");
    }
}
于 2015-05-22T17:57:23.880 回答
0

因此,我刚刚阅读了有关如何从通知中心关闭/删除所有已触发的本地通知的线程,如果用户通过单击应用程序图标而不是通知来打开应用程序。但毕竟这一切,其他计划的本地通知应该在未来触发。

这是我的简单解决方案,应该在应用程序变为Active时触发:

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

我试过 [[UIApplication sharedApplication] cancelLocalNotification:notification]; 但它没有从通知中心(应用程序外部)清除已经触发的本地通知。

于 2018-04-27T14:30:59.337 回答