gpt4 book ai didi

ios - 如何在 iOS 10 中为推送通知设置角标(Badge)计数?

转载 作者:行者123 更新时间:2023-11-28 10:16:15 29 4
gpt4 key购买 nike

我想在收到通知时在应用程序图标上设置角标(Badge)计数。收到通知时调用以下方法。但角标(Badge)计数未设置。

     func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

var userData = userInfo as NSDictionary? as? [AnyHashable: Any] ?? [:]
var aps = userData["aps"] as! NSDictionary
var badge = aps["badge"]
print("data is \(badge!)")
UIApplication.shared.applicationIconBadgeNumber = badge! as! Int
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.badge = 10
} else {
// Fallback on earlier versions
}
// your badge count
}

最佳答案

实际上在 iOS 10 中这两个方法被称为:

如果应用在前台:

   func userNotificationCenter(_ center: UNUserNotificationCenter, 
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

如果应用程序在后台:

   func userNotificationCenter(_ center: UNUserNotificationCenter, 
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)

需要获取notificationrequestcontent

  • 前景法

    let content = notification.request.content
  • 后台方法

    let content = response.notification.request.content

角标(Badge)编号在content的属性badge

let badgeNumber = content.badge as! Int

关于ios - 如何在 iOS 10 中为推送通知设置角标(Badge)计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41103079/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com