我使用FCM在应用程序中收到通知,但由于通知中似乎缺少变量,我无法在应用程序图标上显示徽章计数。我该如何添加?
下面是我得到的错误
[AnyHashable("icon"): logo.png, AnyHashable("user_id"): 3, AnyHashable("google.c.sender.id"): 938224596608, AnyHashable("aps"): {
alert = {
body = "You have new message! Check in the app.";
title = Tutil;
};
sound = default;
}, AnyHashable("google.c.a.e"): 1, AnyHashable("body"): You have new message! Check in the app., AnyHashable("user_photo"): images/3.png, AnyHashable("type"): message, AnyHashable("gcm.message_id"): 1604897386664267, AnyHashable("title"): Tutil, AnyHashable("user_name"): Mali, AnyHashable("lessons"): []]
下面是我的代码
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
print("%@", userInfo)
// Print message ID.
let content = notification.request.content
let badgeCount = content.badge as! Int //<<i am getting my error here as variables may not be available
incrementBadgeNumberBy(badgeNumberIncrement: badgeCount)
completionHandler([.alert,.badge,.sound])
}
您可以获得类似的徽章
if let aps = userInfo["aps"] as? Dictionary<String, Any>, let badge = aps["badge"] as? Int {
print(badge)
}