Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

userInfo notification data not retrievable?

So I have been trying to figure out why userInfo has not been displaying the data I want. When I print userInfo I get this:

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

However when I try to do userInfo["alert"] it returns nil. I have tried everything including userInfo["aps"]["alert"] but still nothing. I have been stuck for hours now. I hope someone can help.

Here is my full function:

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)
    }
}
like image 790
Nathan Modern Avatar asked May 06 '26 05:05

Nathan Modern


1 Answers

As you can see in the userInfo dump badge is in a dictionary for key aps

if let aps = userInfo["aps"] as? [String:Any] {
  let badgeNumber = aps["badge"] as! Int
  application.applicationIconBadgeNumber = badgeNumber
}
like image 96
vadian Avatar answered May 09 '26 12:05

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!