[英]Does Firebase push-notification badge count increase automatic in iOS?
I am getting remote push-notification from firebase.我收到来自 firebase 的远程推送通知。 I am trying to get badge count in app icon.
我正在尝试在应用程序图标中获取徽章计数。 In Firebase there is option to give badge count as bellow
在 Firebase 中,可以选择将徽章计数如下所示
As for now i dont have device to test.至于现在我没有要测试的设备。 My question is if i put 1 as badge count every-time will it increment badge count automatically on app icon?
我的问题是,如果我每次都将 1 作为徽章计数,它会在应用程序图标上自动增加徽章计数吗? if no then how to increment it using firebase.
如果没有,那么如何使用 firebase 增加它。
You want to use UserDefaults
to keep track of the amount of notifications that come in您想使用
UserDefaults
来跟踪传入的通知数量
1- first register the badge count to UserDefaults
with a value of 0
. 1-首先将徽章计数注册到
UserDefaults
,值为0
。 I usually register on Login Screen in viewDidLoad with whatever other values that I need to register我通常在 viewDidLoad 的登录屏幕上注册我需要注册的任何其他值
var dict = [String: Any]()
dict.updateValue(0, forKey: "badgeCount")
UserDefaults.standard.register(defaults: dict)
2- when your notification comes in from Firebase to your app, update the "badgeCount"
. 2-当您的通知从 Firebase 发送到您的应用程序时,更新
"badgeCount"
。 Here is an example when the notification comes in to AppDelegate
:这是通知进入
AppDelegate
时的示例:
// this is inside AppDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// A. get the dict info from the notification
let userInfo = notification.request.content.userInfo
// B. safely unwrap it
guard let userInfoDict = userInfo as? [String: Any] else { return }
// C. in this example a message notification came through. At this point I'm not doing anything with the message, I just want to make sure that it exists
guard let _ = userInfoDict["message"] as? String else { return }
// D. access the "badgeCount" from UserDefaults that you registered in step 1 above
if var badgeCount = UserDefaults.standard.value(forKey: "badgeCount") as? Int {
// E. increase the badgeCount by 1 since one notification came through
badgeCount += 1
// F. update UserDefaults with the updated badgeCount
UserDefaults.standard.setValue(badgeCount, forKey: "badgeCount")
// G. update the application with the current badgeCount so that it will appear on the app icon
UIApplication.shared.applicationIconBadgeNumber = badgeCount
}
}
3- whatever logic in whichever vc that you use to acknowledge when the user has viewed the notification, reset UserDefaults'
badgeCount back to zero. 3-无论您在用户查看通知时用于确认的任何 vc 中的任何逻辑,都将
UserDefaults'
badgeCount 重置为零。 Also set the UIApplication.shared.applicationIconBadgeNumber
to zero UIApplication.shared.applicationIconBadgeNumber
设置为零
SomeVC:一些VC:
func resetBadgeCount() {
// A. reset userDefaults badge counter to 0
UserDefaults.standard.setValue(0, forKey: "badgeCount")
// B. reset this back to 0 too
UIApplication.shared.applicationIconBadgeNumber = 0
}
Information for UIApplication.shared.applicationIconBadgeNumber UIApplication.shared.applicationIconBadgeNumber的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.