【问题标题】:How to Update App Badge with Local Notification如何使用本地通知更新应用程序徽章
【发布时间】:2022-01-21 13:33:33
【问题描述】:

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

当应用在前台时处理通知

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
}

【问题讨论】:

  • 您不会仅仅因为触发了本地通知而收到事件。

标签: ios swift unusernotificationcenter


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-07-19
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2012-12-24
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多