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

How to remove badge but not remove all notifications?

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

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

如果我用

[UIApplication sharedApplication].applicationIconBadgeNumber = 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];
      }
}

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

这是另一个适用于 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
}