How do I access remote push notification data on applicationDidBecomeActive?
Asked Answered
I

4

7

When receiving a remote push notification as the application is in the background, the app enters applicationDidBecomeActive. From there, how can I access the NSDictionary of data from the notification?

Impulsion answered 18/7, 2011 at 11:48 Comment(0)
R
14

The notification data is delivered to your app in application:didReceiveRemoteNotification:. If you want to process it in applicationDidBecomeActive: you should store it in application:didReceiveRemoteNotification: and read it again in applicationDidBecomeActive.

Recitativo answered 18/7, 2011 at 11:52 Comment(2)
Sorry to revive this, but what happens when you receive the notification on application:didReceiveRemoteNotification and your application is in the foreground, which means applicationDidBecomeActive will not be called.Marcus
In application:didReceiveRemoteNotification: you can query -[UIApplication applicationState] to find out if your app is already in the foreground. If it is then just process the notification right away.Recitativo
A
2

Swift version:

var dUserInfo: [NSObject : AnyObject]?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// code...

if let info = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
        dUserInfo = info
    }

    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    dUserInfo = userInfo
}

func applicationDidBecomeActive(application: UIApplication) {
    // code...

    self.yourAction(dUserInfo)
}

func yourAction(userInfo: [NSObject : AnyObject]?) {
    if let info = userInfo?["aps"] as? Dictionary<String, AnyObject> {
    }
}
Arguello answered 18/5, 2015 at 13:39 Comment(0)
D
1

I use this code to manage the push:

In the AppDelegate

@implementation AppDelegate{
    NSDictionary *dUserInfo; //To storage the push data
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Check for options
    if (launchOptions != nil)
    {
        //Store the data from the push.
        dUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dUserInfo != nil)
        {
            //Do whatever you need
        }
    }

    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{   
    //Data from the push.
    if (dUserInfo != nil)
    {
        //Do whatever you need
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //Store the data from the push.
    if (userInfo != nil)
    {
        dUserInfo = userInfo;
    }
}

I hope this will be useful for someone.

Happy coding.

Demi answered 4/2, 2015 at 16:20 Comment(0)
C
0

If your app is in background state when push notification is received and tapped, the app will get invoked with application:didFinishLaunchingWithOptions: and not application:didReceiveRemoteNotification:.

Push notification payload can be accessed in application:didFinishLaunchingWithOptions: from launchOptions dictionary.

Commie answered 1/6, 2014 at 9:14 Comment(1)
This is not correct application:didFinishLaunchingWithOptions: is not called when the app is in background, this is only called when the application starts. what you are saying is totally oposite, first it will be called application:didReceiveRemoteNotification: and then applicationDidBecomeActive:Demi

© 2022 - 2024 — McMap. All rights reserved.