gpt4 book ai didi

ios - 如何在收到推送通知时更新角标(Badge)号码

转载 作者:行者123 更新时间:2023-12-01 15:55:48 30 4
gpt4 key购买 nike

我需要帮助完成这段代码,以便应用程序在收到推送通知时显示角标(Badge)编号,我可以收到推送通知,但应用程序上没有角标(Badge),这是代码

 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. 前台方法
  2. 后台方法

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

获取角标(Badge)计数

let badge = content.badge as! Int

关于ios - 如何在收到推送通知时更新角标(Badge)号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46598362/

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