用户通知框架徽章不会增加

我在我的应用程序中使用UserNotification框架,我想将徽章设置为收到的通知数量,所以我所做的是将收到的通知数量设置为用户默认值然后我尝试将值分配给徽章以便让我徽章编号但徽章编号不会增加。 这是我的代码

设置接收通知的值

 center.getDeliveredNotifications { notification in UserDefaults.standard.set(notification.count, forKey: Constants.NOTIFICATION_COUNT) print("notification.count \(notification.count)") print(".count noti \(UserDefaults.standard.integer(forKey: Constants.NOTIFICATION_COUNT))") } 

这准确地打印了收到的通知数量,当我决定将其设置为我的徽章时,它只显示1

 content.badge = NSNumber(value: UserDefaults.standard.integer(forKey: Constants.NOTIFICATION_COUNT)) 

我不知道为什么价值不会每次都增加。 任何帮助,将不胜感激。

或者,如果可以在应用程序的任何位置始终更新徽章。

我假设这是一个本地通知。

AFAIK有你问题的解决方案!

通知到达时,您处于前台或后台。

  • 前景:你得到了userNotificationCenter(_:willPresent:withCompletionHandler:)回调,但我不认为在这种情况下你会想要增加徽章吗? 因为用户刚看到它。 虽然我可以想象你可能需要做什么。 假设您的应用程序类似于WhatsApp,并且用户已打开该应用程序并正在向其母亲发送消息。 然后他父亲的消息到了。 此时他还没有打开他和他父亲之间的消息,但他看到了通知。 在您的willPresent中,您可以查询getDeliveredNotifications并调整您的徽章数量。
  • 背景:对于iOS10 +本地通知版本,你运气不好! 因为没有回电给你。 通知会传递给操作系统,就是这样! 有一个命名的application:didReceiveLocalNotification:不推荐使用 。 有关详细信息,请参阅此处
  • 当用户点击(前台或后端)然后你将获得userNotificationCenter(_:didReceive:withCompletionHandler:)但是再没有用,因为用户已经确认收到通知并且在这种情况下增加徽章没有意义。

长话短说AFAIK没有什么可以做本地通知。

如果它是远程通知,那么在application(_:didReceiveRemoteNotification:fetchCompletionHandler:)您可以查询已发送的通知并增加徽章数…

像这样发送本地通知:

 func sendNotification(title: String, subtitle: String, body: String) { let center = UNUserNotificationCenter.current() center.getPendingNotificationRequests(completionHandler: { pendingNotificationRequests in let content = UNMutableNotificationContent() content.title = title content.subtitle = subtitle content.body = body DispatchQueue.main.sync { content.badge = NSNumber(integerLiteral: UIApplication.shared.applicationIconBadgeNumber + pendingNotificationRequests.count + 1) let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 4, repeats: false) let requestIdentifier = UUID().uuidString //You probably want to save these request identifiers if you want to remove the corresponding notifications later let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger) center.add(request, withCompletionHandler: { (error) in // Handle error }) } }) } 

(您可能需要保存请求的标识符(如果您想更新它们,可以在用户默认值或核心数据中保存,或者甚至通过removePendingNotificationRequests(withIdentifiers:)取消它们)

将您的视图控制器声明为UNUserNotificationCenterDelegate

 class ViewController: UIViewController, UNUserNotificationCenterDelegate { override func viewDidLoad() { super.viewDidLoad() UNUserNotificationCenter.current().delegate = self } //... } 

要处理与通知的互动,请更新应用的徽章以及即将发布的通知的徽章:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { DispatchQueue.main.async { UIApplication.shared.applicationIconBadgeNumber -= 1 } let center = UNUserNotificationCenter.current() center.getPendingNotificationRequests(completionHandler: {requests in let newRequests: [UNNotificationRequest] = requests.map { request in let newContent: UNMutableNotificationContent = request.content as! UNMutableNotificationContent newContent.badge = (Int(truncating: request.content.badge ?? 0) - 1) as NSNumber let newRequest: UNNotificationRequest = UNNotificationRequest(identifier: request.identifier, content: newContent, trigger: request.trigger) return newRequest } newRequests.forEach { center.add($0, withCompletionHandler: { (error) in // Handle error }) } }) completionHandler() } 

当通知与(收到/打开)交互时,通过减少应用徽章来更新应用徽章。 此外,它还会更新待处理通知的内容徽章。 添加具有相同标识符的通知请求只会更新待处理通知。

要在前台接收通知,并在通知未与之交互时增加应用徽章图标,请执行以下操作:

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { DispatchQueue.main.async { UIApplication.shared.applicationIconBadgeNumber += 1 } completionHandler([.alert, .sound]) } 

这是一些GIF:

  • 1st :接收本地通知会增加应用徽章。 而与通知进行交互会减少应用徽章。

  • 第二 :在应用程序被杀死时接收本地通知( 我在此使用了15秒的触发timeInterval )。

  • 第3步 :在前台接收通知会增加应用程序徽章,除非用户与之交互。