Reputation: 87
How can I make a background API call on receiving notification to update tab bar badge value in swift 2.3?
I am using following code to update badge value on remote notification:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
pushDictionary = userInfo
NSNotificationCenter.defaultCenter().postNotificationName("PushNotification", object: self, userInfo: userInfo)
let pushNotification = PushNotification(dictionary: pushDictionary!)
notification.subtitleText = pushNotification.message
if application.applicationState == UIApplicationState.Inactive {
if pushNotification.type != nil {
if pushNotification.type == "vc1"{
self.tabBarViewController.selectedIndex = 0
self.tabBarViewController.tabBar.items![0].badgeValue = "1"
self.tabBarViewController.selectedViewController?.viewWillAppear(true)
}
if pushNotification.type == "vc2"{
self.tabBarViewController.selectedIndex = 1
}
if pushNotification.type == "vc3"{
self.tabBarViewController.selectedIndex = 2
}
}
}
I want then badge value to be incremented on receiving notification if the user is in foreground and background. Thanks in advance
I am updating the badge value in ViewController:
let unreadFeeds = feedsDictionary?.objectForKey("UnReadFeeds") as! NSInteger
if unreadFeeds > 0 {
self.tabBarController?.tabBar.items?[0].badgeValue = String(unreadFeeds)
if #available(iOS 10.0, *) {
self.tabBarController?.tabBar.items![0].badgeColor = UIColor(red: 219/255.0, green: 90/255.0, blue: 41/255.0, alpha: 1)
} else {
// Fallback on earlier versions
}
Upvotes: 1
Views: 2797
Reputation: 8516
Whenever user get notification, then you will get callback on Remote notification delegate method. Update the tab bar badge there as below.
self.tabBarController?.tabBar.items![0].badgeValue = "YourBadgeValue"
if your view controller is inside a navigation controller
, you have to use
self.navigationController!.tabBarItem.badgeValue = "YourBadgeValue"
Upvotes: 3