0

I have notification badges for two of my tabs, at the 1 position and the 2 position of the UITabBarController. The badges for the number 1 position show fine and even if the app is open it will show the new badge number. However the 2 position tab shows once, and when the app is open or even closed it won't show the new badge number. It only will show the badge number after hours of inactivity. Would this be an issue with my code or a problem on my backend? The 1 position is request notifications and 2 position is chat messages.

enter image description here

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
}


 }
hg56
  • 185
  • 1
  • 3
  • 16
  • 1
    Just a hunch that you might be in dispatch queue other then UI, try replacing `self.setApplictionBadgeCount()` to `DispatchQueue.main.async{ self.setApplicationBadgeCount() }` – AamirR Jan 30 '18 at 20:43
  • the badge number wouldn't go away after trying this. – hg56 Jan 30 '18 at 20:55

1 Answers1

0

Is there actually a number of unseen conversations being returned? If it's nil, then there will not be a badge. Double check that, and even try setting it to a hard-coded string for a quick and dirty test. Also, are you on tab 1 when trying to see the updated badge on tab 2? Try changing the tab 2 badge when you have that tab selected. You might also want to make sure that self.tabBar.items?[2] is not nil.

casperson
  • 146
  • 12
  • It only shows it once I stop the app from running and rerun it from Xcode. Then it shows the badge on the 2 position. It doesn't return nil. – hg56 Jan 30 '18 at 20:38