【问题标题】:userInfo notification data not retrievable?userInfo 通知数据不可检索?
【发布时间】:2016-12-07 18:32:04
【问题描述】:

所以我一直试图弄清楚为什么 userInfo 没有显示我想要的数据。 当我打印 userInfo 我得到这个:

[AnyHashable("aps"): {
    alert = "test account \ni'm running late";
    badge = 11;
    sound = "bingbong.aiff";
}]

但是,当我尝试执行 userInfo["alert"] 时,它返回 nil。我已经尝试了一切,包括userInfo["aps"]["alert"],但仍然没有。我已经被困了好几个小时了。我希望有人能帮帮忙。

这是我的全部功能:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    //PFPush.handle(userInfo)
    if (userInfo["badge"] != nil) {
        let badgeNumber: Int = userInfo["badge"] as! Int
        application.applicationIconBadgeNumber = badgeNumber
    }
    if application.applicationState == .inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(inBackground: userInfo, block: nil)
    } else if application.applicationState == .active {
        let notificationManager = LNRNotificationManager()
        notificationManager.showNotification(title: "Test", body: "", onTap: { () -> Void in
            notificationManager.dismissActiveNotification(completion: { () -> Void in
                print("Notification dismissed")
            })
        })
        print(userInfo)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "didRecieveNotificationWhileInApp"), object: nil)
    }
}

【问题讨论】:

    标签: swift xcode push-notification apple-push-notifications


    【解决方案1】:

    正如您在 userInfo 转储中看到的那样,badge 在字典中的键 aps

    if let aps = userInfo["aps"] as? [String:Any] {
      let badgeNumber = aps["badge"] as! Int
      application.applicationIconBadgeNumber = badgeNumber
    }
    

    【讨论】:

    • 哇,非常感谢。你不知道我努力让它工作多久了哈哈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    相关资源
    最近更新 更多