[英]Firebase Push Notification Increase Badge Count When Receive Notification
[英]Increase local notification badge count past 1
我目前正在嘗試將本地通知實施到我的應用程序中,但遇到了由於某種原因無法將徽章計數器增加到 1 以上的問題。
這是我配置和安排通知的方法。
func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "\(self.title == "" ? "Title" : self.title) is done"
content.subtitle = "Tap to view"
content.sound = UNNotificationSound.default
content.badge = 1
if self.isPaused {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: self.currentTime, repeats: false)
let request = UNNotificationRequest(identifier: self.notificationIdentifier.uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
removeNotification()
}
}
出於某種原因,當多個通知被成功安排並確實發送時,徽章計數器只會增加到 1,而不管實際發送的通知數量是多少。
有沒有一種正確的方法來管理徽章數量,這不是嗎?
你應該考慮一下你的代碼做了什么。 您沒有增加徽章數量,只是每次將其設置為 1。
這是實現徽章計數的一種方法:
您需要一種方法來跟蹤當前的徽章計數。 一種簡單的解決方案是使用用戶默認值。
當您安排新通知時,您需要增加徽章計數而不是將其設置為靜態值。
您應該為通知設置增加的徽章計數。
當應用程序打開時,您應該將徽章計數重置為零。
func scheduleNotifications(notificationBody: String, notificationID: String) { //Your other notification scheduling code here... //Retreive the value from User Defaults and increase it by 1 let badgeCount = userDefaults.value(forKey: "NotificationBadgeCount") as! Int + 1 //Save the new value to User Defaults userDefaults.set(badgeCount, forKey: "NotificationBadgeCount") //Set the value as the current badge count content.badge = badgeCount as NSNumber }
在您的application(_:didFinishLaunchingWithOptions:)
方法中,您在應用啟動時將徽章計數重置為零:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.applicationIconBadgeNumber = 0
userDefaults.set(0, forKey: "NotificationBadgeCount")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.