如何使用本地通知更新应用程序徽章



我使用本地通知向用户传递消息,同时我想在通知触发时更新应用程序徽章,但本地通知代理具有在应用程序处于前台以及用户与通知交互时处理通知的功能(比如点击它(。当通知触发并且应用程序处于后台时,有什么方法可以更新应用程序徽章吗?

当应用程序处于前台时处理通知

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// run code when app in foreground

}

当点击时处理通知

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// run code when user interact with notification only
}

要在本地通知触发时更新应用程序徽章,请在设置通知时在下面的代码中设置徽章值:

func schedulNotification() {
let content = UNMutableNotificationContent()
content.badge = 1  // That will appear on app icon when notification trigger
// Continue notification settings...
}

最新更新