ios - 如何在收到通知时更新 tabBar 角标(Badge)值

标签 ios swift2 xcode8

如何在接收通知时进行后台 API 调用以更新 swift 2.3 中的标签栏角标(Badge)值?

我正在使用以下代码来更新远程通知上的角标(Badge)值:

    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
            }
        }
    }

如果用户在前台和后台,我希望在收到通知时增加角标(Badge)值。提前致谢

我正在更新 ViewController 中的角标(Badge)值:
    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
                }

最佳答案

每当用户收到通知时,您都会收到远程通知委托(delegate)方法的回调。如下更新标签栏角标(Badge)。

self.tabBarController?.tabBar.items![0].badgeValue = "YourBadgeValue"

如果您的 View Controller 在 navigation controller 内,你必须使用
self.navigationController!.tabBarItem.badgeValue = "YourBadgeValue"

关于ios - 如何在收到通知时更新 tabBar 角标(Badge)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42352214/

相关文章:

ios - 在 GLKit View 中实现持久化效果

Swift 2 - 在 if 语句上使用 break 的用例?

swift - 为什么调用 audioplayer.play(atTime : delay) makes no sound regardless of the value of delay

objective-c - 减去日期和时间

ios - 是否可以在 Mac Catalyst 中使用全屏?

ios - 如何在单例方法中实现 AVAudioPlayer?

ios - 在 iOS 中使用 React Native 时出现意外字符 ""

uisearchbar - SearchController SearchBar 从 View 中消失

ios - 从服务器发送和获取设备位置

objective-c - 如何使用 swift 2.0 在 Xcode 7 beta 6 (7A192o) 中桥接 Objective-C?