【问题标题】:TabItem with BadgeValue only shows up when I launch the app from the notification trigger只有当我从通知触发器启动应用程序时,才会显示带有 BadgeValue 的 TabItem
【发布时间】:2017-12-21 09:54:42
【问题描述】:

我有一个使用 PHP EasyAPNS 通知的推送通知应用程序在 Swift 3、iOS 10 上运行良好。但我无法理解的一件事是,为什么当我从通知警报启动应用程序时 TabItem 上的徽章工作正常但是不是当我直接从应用程序图标(带有红色徽章)打开应用程序时

这是我在 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)
        }
    }
}

所以,当我点击警报打开我的应用程序时,徽章就像这样:

但是当我使用图标本身打开应用程序时,徽章没有出现:

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

如果我能改进这个问题,请告诉我!

【问题讨论】:

  • 不确定这是否与您的问题有关,但您使用的他的功能已弃用。你不应该在 iOS10 中使用这个功能。如果它有效,那是因为你很幸运。见herehere
  • 在iOS10中,如果你想在用户点击时有回调,那么你必须使用didReceiveNotificationResponse

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


【解决方案1】:

您应该使用application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 方法来处理通知。如文档 (found here) 中所述,无论应用程序在前台还是后台,都会调用此方法。

同样值得注意的是application(_:didReceiveRemoteNotification:)的文档

如果远程通知到达时应用程序未运行,则 方法启动应用程序并在 启动选项字典。该应用程序不会调用此方法来 处理那个远程通知。

注意,如果应用程序没有运行并且用户点击图标,应用程序将调用application(_:didFinishLaunchingWithOptions:)。如果应用有需要处理的远程通知,则会有适当的 launchOption 键值对。

【讨论】:

  • 当我单击通知以转到 swift 3 中的特定视图控制器时出现错误,错误 i:-could not cast value of type 'UINavigationController' (0x3a79b0a0) to 'UITabBarController' (0x3a79b938) ) 应用程序崩溃@lostIn Transit
  • 那将是您代码中的内容。你得到一个UINavigationController,但试图将它用作UITabBarController。检查您的实施。这可能应该作为另一个问题。它与通知无关。
  • 你能帮我解决这个@lostIn Transit
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 2017-07-30
  • 2023-04-08
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多