简体   繁体   English

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

[英]Removing UILocalNotification from notification tray programmatically

Is there a way to programmatically remove/dismiss UILocalNotification from Notification Tray. 有没有办法以编程方式从通知托盘中删除/关闭UILocalNotification I am able to cancel the notification which removes the notifications from 我可以取消删除通知的通知

[[UIApplication sharedApplication] scheduledLocalNotifications]

Here is what i need to do 这是我需要做的

I need to dismiss the UILocalNotification from NotificationTray after the action has been performed(ie after the user taps the notification) 我需要在执行操作后(即在用户点击通知后)从NotificationTray中UILocalNotification

EDIT: I can remove the notifications from the NSNotificationCenter . 编辑:我可以从NSNotificationCenter删除通知。 I want to remove specific notifications from the Notification Tray .Like the user presses the clear button to clear all the notifications belonging to a particular application. 我想从通知托盘中删除特定通知。例如,用户按下清除按钮以清除属于特定应用程序的所有通知。

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. 如果要删除特定通知,可以使用通知对象的userinfo ,在创建本地通知时为其添加唯一ID。 Later you can use that ID for removing local notification. 稍后您可以使用该ID删除本地通知。

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

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. 但是,似乎scheduledLocalNotifications不包括其发布日期已经过去的位置,即使它们仍出现在通知中心中。

Calling cancelAllLocalNotifications does seem to remove past notifications as well. 调用cancelAllLocalNotifications似乎也会删除过去的通知。 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 此外,如果不再需要某些通知,我们可以在添加它们之前对其进行一些过滤,并且我们可以在应用程序变为活动状态时获取活动通知,将它们存储在实例变量中,并且仅在应用程序移动到时才将其添加回来的背景

[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 :) 但如果你没有使用applicationIconBadgeNumber,它将无法正常工作,所以首先设置applicationIconBadgeNumber技巧:)

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];

If the Application is not running, you will be receiving the Local Notification object in the 如果应用程序未运行,您将在。中收到本地通知对象

-applicationDidFinishLaunchingWithOptions: -applicationDidFinishLaunchingWithOptions:

like: 喜欢:

 UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

or else you can get it in 或者你可以得到它

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

Now you can remove it from the Notification Center using 现在,您可以使用将其从通知中心中删除

[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel]; [[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];
  }
}

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; 这可能是因为Apple不知道你在做什么,老实说也不在乎; 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");
    }
}

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]; 我试过[[UIApplication sharedApplication] cancelLocalNotification:notification]; but it did not clear the already fired local notifications from the Notification center (outside of the app). 但它没有清除通知中心(应用程序外部)已经解雇的本地通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM