【问题标题】:How to remove badge but not remove all notifications?如何删除徽章但不删除所有通知?
【发布时间】:2017-06-26 03:52:50
【问题描述】:

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

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

如果我使用

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

它会清除所有收到的通知。

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

【问题讨论】:

  • @ Nathanwhy :- 当您在通知中心点击它以打开应用程序时,SpringBoard 似乎会自动关闭通知。以编程方式您无法删除特定的远程通知

标签: ios uilocalnotification


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    好的,我在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];
          }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      相关资源
      最近更新 更多