如何在 iOS13 中收到推送通知时自动增加通知徽章编号

Enr*_*dez 5 iphone apple-push-notifications ios swift ios13

在 iOS 13 之前,当推送通知到达方法时,我会增加应用程序徽章编号application :didReceiveRemoteNotification:fetchCompletionHandler

    func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == .background {
            UIApplication.shared.applicationIconBadgeNumber += 1
        }
        completionHandler(.newData)
    } 
Run Code Online (Sandbox Code Playgroud)

然后我每次打开应用程序时都会清理徽章号码:

   func applicationDidBecomeActive(_ application: UIApplication) {
       UIApplication.shared.applicationIconBadgeNumber = 0
   }
Run Code Online (Sandbox Code Playgroud)

问题是,根据iOS 13 中的最新更改,application :didReceiveRemoteNotification:fetchCompletionHandler仅当推送通知的属性设置为至少 5 时才会调用该方法apns-priority。我们使用的第三方推送通知服务不会发送具有该属性的推送通知。

所以我的问题是:

是否有其他方法可以在收到推送通知时自动增加应用程序徽章编号?

谢谢

Joh*_*ion 0

您不需要唤醒应用程序并执行此操作,这将由系统决定是否唤醒您的应用程序。最好让系统处理它,并在通知中包含徽章编号。

https://developer.apple.com/documentation/usernotifications/unnotificationcontent/1649864-badge?language=objc