ios - 带有 BadgeValue 的 TabItem 仅在我从通知触发器启动应用程序时显示

标签 ios swift3 push-notification apple-push-notifications ios10

我有一个使用 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)就很好,如下所示:

TabItem Badge working

但是当我使用图标本身打开应用程序时,角标(Badge)没有显示:

TabItem Badge NOT working

有人知道我做错了什么吗?

如果我可以改进问题,请告诉我!

最佳答案

您应该使用 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/

相关文章:

android - 你如何告诉 Xamarin Studio 为构建提供详细输出?

ios - 使用 Expandable UITableView 只显示一行

ios - 增加 UITableView 的透明度,因为它滚动到内容的边缘之外

android - Android中推送通知的困惑

ios 10 Firebase 推送通知委托(delegate)方法未在后台模式下调用

Azure NotificationHub - 检测失败的通知

ios - 折叠可折叠部分标题时下拉 UITableView 表标题

ios - 在 AppDelegate 中检测抖动

swift3 - CATransform3D 关键路径与#keyPath

swift - Swift 中的循环迭代器