我有一个使用 PHP EasyAPNS 通知推送通知的应用程序在 Swift 3、iOS 10 上运行良好。但我无法理解的一件事是为什么当我从通知警报启动应用程序时 TabItem 上的角标(Badge)运行良好但是当我直接从应用程序图标(带有红色角标(Badge))打开应用程序时不是这样
这是我在 AppDelegate 上使用的代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [String:Any])
{
print("Message details \(userInfo)")
if let aps = userInfo["aps"] as? NSDictionary
{
if let alertMessage = aps["alert"] as? String {
let rootViewController = self.window?.rootViewController as! UITabBarController!
let tabArray = rootViewController?.tabBar.items as NSArray!
let tabItem = tabArray?.object(at: 3) as! UITabBarItem
tabItem.badgeValue = "1"
let myAlert = UIAlertController(title: "Message", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)
myAlert.addAction(okAction)
self.window?.rootViewController?.present(myAlert, animated: true, completion: nil)
}
}
}
因此,当我单击警报打开我的应用程序时,角标(Badge)就很好,如下所示:
但是当我使用图标本身打开应用程序时,角标(Badge)没有显示:
有人知道我做错了什么吗?
如果我可以改进问题,请告诉我!
最佳答案
您应该使用 application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
方法来处理通知。正如文档 ( found here ) 中所述,无论应用程序位于前台还是后台,都会调用此方法。
在 application(_:didReceiveRemoteNotification:) 的文档中还值得注意
If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification.
请注意,如果应用未运行且用户点击图标,应用将调用 application(_:didFinishLaunchingWithOptions:)
。如果应用程序有需要处理的远程通知,将会有适当的 launchOption 键值对。
关于ios - 带有 BadgeValue 的 TabItem 仅在我从通知触发器启动应用程序时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45134446/