用户在后台模式下收到通知时如何更改徽章编号

时间:2018-10-25 22:04:25

标签: ios swift

 func application(_ application: UIApplication, didReceiveRemoteNotification notification: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    /*
        let aps = userInfo["aps"] as! [String: AnyObject]
        if let count = aps["badge"] as? Int {
            application.applicationIconBadgeNumber = 2
        }
    */

    let custom = notification["custom"] as! [String: AnyObject]

    if let home = custom["a"]!["home"] as? String, home == "1" {
        incrementBadgeNumberBy(badgeNumberIncrement: 1)
    }
}

1 个答案:

答案 0 :(得分:2)

在通用应用程序徽章中,是从推送通知有效负载中设置的。

有效负载示例:

{
   “aps” : {
      “badge” : 9
   },
}

您还可以使用设置徽章

UIApplication.shared.applicationIconBadgeNumber = 3