Nathan Modern
Nathan Modern

Reputation: 175

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)
    }
}

Upvotes: 3

Views: 309

Answers (1)

vadian
vadian

Reputation: 285059

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
}

Upvotes: 4

Related Questions