我正在努力使类似iOS Messenger的应用程序的applicationIconBadgeNumber自身递增-当该应用程序收到诸如gmail或facebook-messenger之类的推送通知时为未读数字。
我通过实现静默推送通知和常规推送通知来实现此目的,如下所示。当发布新消息时,无提示和正常的推送通知将发送到设备。
// AppDelegate.swift
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
UIApplication.shared.applicationIconBadgeNumber += 1
completionHandler(.noData)
}
~~~
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping(UNNotificationPresentationOptions) -> Void) {
if notification.request.trigger is UNPushNotificationTrigger {
completionHandler([.sound, .alert])
}
}
还有其他更聪明的方法吗?由于服务器没有每个用户的未读号码,因此每个设备都需要在其中包含该号码。