我有两个选项卡的通知徽章,位于UITabBarController的1位置和2位置。 1号位置的徽章显示正常,即使应用程序处于打开状态,它也会显示新的徽章编号。但是,2位置标签显示一次,当应用程序打开或甚至关闭时,它不会显示新的徽章编号。它只会在几小时不活动后显示徽章编号。这会是我的代码问题还是我后端的问题? 1位是请求通知,2位是聊天消息。
class NewTabBarViewController: UITabBarController {
var numPendingRequests = 0
var numUnseenConversations = 0
override func viewDidLoad() {
super.viewDidLoad()
observeNumPendingRequests()
observeNumUnseenConversations()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
sendFCMToken()
observeNumPendingRequests()
observeNumUnseenConversations()
AuthService().getCurrentUserInfo()
// Show notification center if the app launched from a notification
// Typically from the background or killed app state
if NotificationService.launchedFromNotification {
showNotificationCenter()
NotificationService.launchedFromNotification = false
}
// Observe if the app is opened from a notification
// Typically from the foreground state
NotificationCenter.default.addObserver(self, selector: #selector(showNotificationCenter), name: NotificationService.didLaunchFromNotification, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Stop observing if the app is opened from a notification
NotificationCenter.default.removeObserver(self, name: NotificationService.didLaunchFromNotification, object: nil)
}
// Select the tab item with the notification center
@objc func showNotificationCenter() {
self.selectedIndex = 2
}
/**
Sends the users private Firebase Cloud Messaging Token to the database
to be used by the Cloud Functions for sending Push Notifications
*/
func sendFCMToken() {
guard let token = Messaging.messaging().fcmToken, let user = Auth.auth().currentUser else { return }
let db = Database.database().reference()
let ref = db.child("FCMToken/\(user.uid)")
ref.setValue(token)
}
/**
Observes the number of received requests that are PENDING
and updates the App Badge Icon Number and tabBar Item accordingly
*/
func observeNumPendingRequests() {
guard let user = Auth.auth().currentUser else { return }
let db = Database.database().reference()
let ref = db.child("receivedRequests/\(user.uid)").queryOrderedByValue().queryEqual(toValue: "PENDING")
ref.observe(.value, with: { snapshot in
self.numPendingRequests = Int(snapshot.childrenCount)
self.setApplicationBadgeCount()
})
}
func observeNumUnseenConversations() {
guard let user = Auth.auth().currentUser else { return }
let db = Database.database().reference()
let ref = db.child("conversations/users/\(user.uid)").queryOrdered(byChild: "seen").queryEqual(toValue: false)
ref.observe(.value, with: { snapshot in
self.numUnseenConversations = Int(snapshot.childrenCount)
self.setApplicationBadgeCount()
})
}
func setApplicationBadgeCount() {
let total = numUnseenConversations + numPendingRequests
UIApplication.shared.applicationIconBadgeNumber = Int(total)
self.tabBar.items?[1].badgeValue = numPendingRequests > 0 ? "\(numPendingRequests)" : nil
self.tabBar.items?[2].badgeValue = numUnseenConversations > 0 ? "\(numUnseenConversations)" : nil
}
}
答案 0 :(得分:0)
实际上是否会返回一些看不见的对话?如果是nil
,则不会有徽章。仔细检查一下,甚至尝试将其设置为硬编码字符串,以进行快速而肮脏的测试。此外,当您尝试在标签2上查看更新的徽章时,您是否在标签1上?选择该选项卡后,尝试更改选项卡2徽章。您可能还需要确保self.tabBar.items?[2]
不是nil
。