如何在接收通知时进行后台 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/