[英]How to update badge number when receive push notification
我需要完成這部分代碼的幫助,因此當收到推送通知時,應用程序將顯示徽章編號,我可以收到推送通知,但應用程序上沒有徽章,這是代碼
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping
(UIBackgroundFetchResult) -> Void) {
if let aps = userInfo["aps"] as? NSDictionary, let _ = aps["alert"] as? String, let sound = aps["sound"] as? String
{
if let systemSound = SystemSoundID(sound) {
AudioServicesPlayAlertSound(systemSound)
}
NotificationCenter.default.post(name: Notification.Name(rawValue: SystemSetting.refreshCouponListNotification), object: nil)
completionHandler(UIBackgroundFetchResult.newData)
}
}
有兩種方法用於推送通知
1,前景法
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
獲取通知請求的內容
let content = notification.request.content
2.背景方法
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
獲取通知請求的內容
let content = response.notification.request.content
獲取徽章計數
let badge = content.badge as! Int
請按照以下步驟在iOS中設置徽章計數:
1.實現邏輯來計算后端的計數並通過Push通知發送該計數,例如:
{ "aps" : { "alert" : "You got your Push Message.", "badge" : 9 } }
2.從推送通知響應中獲取徽章計數:
let pushContent = response.notification.request.content
let badgeCount = content.badge as! Int
3.在didReceiveRemoteNotification方法中設置徽章計數:
UIApplication.sharedApplication().applicationIconBadgeNumber = badgeCount
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.