如何删除徽章但不删除所有通知?

时间:2017-06-26 03:52:51

标签: ios uilocalnotification

如果用户将警报样式设置为横幅。他们可以收到超过1个通知,而不会提示他们清除它。

我看过相同的应用程序,如果点击最新的那个&它打开应用程序,只清除这一个通知,并删除徽章;

如果我使用

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

它将清除已收到的所有通知。

那么如何删除徽章但不删除所有通知?

2 个答案:

答案 0 :(得分:0)

好的,我在this

中找到答案

添加徽章为-1的新通知。

- (void)applicationDidEnterBackground:(UIApplication *)application {
      if (iOS11) {
            UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
            content.badge = @(-1);
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"clearBadge" content:content trigger:nil];
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            }];
      } else {
            UILocalNotification *clearEpisodeNotification = [[UILocalNotification alloc] init];
            clearEpisodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
            clearEpisodeNotification.timeZone = [NSTimeZone defaultTimeZone];
            clearEpisodeNotification.applicationIconBadgeNumber = -1;
            [[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification];
      }
}

然后徽章将被删除,但其他通知不会被删除。

答案 1 :(得分:0)

这是另一个适用于iOS 11的示例(Swift 4.1中的代码):

if #available(iOS 11.0, *) {
   let content = UNMutableNotificationContent()
   content.badge = -1
   let request = UNNotificationRequest(identifier: "clearBadge", content: content, trigger: nil)
   UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
} else {
   UIApplication.shared.applicationIconBadgeNumber = -1
}
相关问题