简体   繁体   English

如何在接收通知时更新tabBar徽章值

[英]How to update tabBar badge value on receiving notification

How can I make a background API call on receiving notification to update tab bar badge value in swift 2.3? 如何在接收swift 2.3中更新标签栏徽章值的通知时进行后台API调用?

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: 我正在更新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
                }

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 如果你的视图控制器在navigation controller ,你必须使用

self.navigationController!.tabBarItem.badgeValue = "YourBadgeValue"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM