I was wondering how I can make use of the payload of a push notification, when the user is launching the app via opening that notification. Is this possible?
I want to implement an action which happens upon opening a notification, but this action requires part of the payload to work.
Cheers
1 Answer 1
Just implement the application(_ application:, didReceiveRemoteNotification userInfo:) in your AppDelegate. The userInfo is a JSON representation of your Push Notification (including the Payload).
Additionally you can check if the App is "inactive", wich means (in this case), that the User just came in to the App via this Push Notification
Example Code:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if (UIApplication.shared.applicationState == .active) {
// you just got a PushNotification, but the app is running currently
} else if (UIApplication.shared.applicationState == .inactive) {
// Do your work in here, the User just opened the App via the Push Notification
// check userInfo for the Payload
}
}
1 Comment
.active part works flawlessly. However what I want the App to do when launching from .inactive or .background doesn't seem to work.