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

时间:2013-01-03 15:57:52

标签: iphone objective-c ios uilocalnotification

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

中删除通知
[[UIApplication sharedApplication] scheduledLocalNotifications]

这是我需要做的事情

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

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

7 个答案:

答案 0 :(得分:16)

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

[[UIApplication sharedApplication] cancelAllLocalNotifications];

如果要删除特定通知,可以使用userinfo通知对象,在创建本地通知时为其添加唯一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];

答案 1 :(得分: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];
}

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

答案 2 :(得分:2)

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

也会做一些技巧

但如果您没有使用applicationIconBadgeNumber,它将无法工作,因此设置了技巧 applicationIconBadgeNumber首先:)

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];

答案 3 :(得分:1)

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

中收到Local Notification对象
  

-applicationDidFinishLaunchingWithOptions:

像:

UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];

或者你可以在

中得到它
  
      
  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
  •   

现在您可以使用

将其从通知中心中删除
  

[[UIApplication sharedApplication]   cancelLocalNotification:notificationToCancel];

答案 4 :(得分: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];
  }
}

答案 5 :(得分:1)

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

就问题而言,我会做以下事情:

html,body{
    margin: 0;
    height: 100%;
    width: 100%;
}

#experiment {
    perspective: 200vw;
    height: 100%; 
    width: 100vw; 
    border: solid 1px blue;
}

.cube {
    position: relative;
    height: 100%; 
    width: 100vw; 
    transform-style: preserve-3d;
}
.face {
    position: absolute;
    height: 100%; 
    width: 100%; 
    color: #fff;
    transition: transform 4s linear;
}
.cube .front {
    transform: translateZ(-50vw) rotateY(0deg) translateZ(50vw); 
    transform-origin: center center;
    background-color:gray;
}

.cube .side {
    transform: translateZ(-50vw) rotateY(-90deg) translateZ(50vw);
    background-color:lightgray;
}

.cube:hover .front {
    transform: translateZ(-50vw) rotateY(90deg) translateZ(50vw); 
}

.cube:hover .side {
    transform: translateZ(-50vw) rotateY(0deg) translateZ(50vw);
}

答案 6 :(得分:0)

所以我刚刚阅读了这个主题,关于如何通过单击应用程序图标而不是通知来打开应用程序,从通知中心关闭/删除所有已经触发的本地通知。但在完成所有这些之后,其他预定的本地通知将在未来启动。

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

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

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

相关问题