iOS React to Push Notification when app is closed

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
I'm using the "didReceiveRemoteNotification"-method to react to a Push Notification when the app is open.

But how can I react to a PN when the app is closed or in the background?

Is there a similar method that is triggered when a user clicks a PN when the app is closed and thus opens it?
 
Last edited:

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
I'm using the "didReceiveRemoteNotification"-method to react to a Push Notification when the app is open.

But how can I react to a PN when the app is closed or in the background?

Is there a similar method that is triggered when a user clicks a PN when the app is closed and thus opens it?
In the method in the appdelegate, where you receive the PN.
Check the applicaiton state of your app (it's possible, i'll leave the googling to you).
Then check if it's ACTIVE, then show a normal alert :) if it isn't, then you'll receive the PN ;)
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Well, the only function that receives the PN that I know of is "didReceiveRemoteNotification", and that only happens when the app is running in either the foreground or background.

So sure, I've implemented the "applicationDidBecomeActive"-method and are able to react to a PN in both these states.

But what I really wanted was to be able to react to a PN when the app is entirely closed (not running in the background) and the user opens the app by clicking the PN (not the app-icon). Is this not possible?
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Well, the only function that receives the PN that I know of is "didReceiveRemoteNotification", and that only happens when the app is running in either the foreground or background.

So sure, I've implemented the "applicationDidBecomeActive"-method and are able to react to a PN in both these states.

But what I really wanted was to be able to react to a PN when the app is entirely closed (not running in the background) and the user opens the app by clicking the PN (not the app-icon). Is this not possible?
This is done by OS..
And you don't need the applicationDidBecomeActive method.
In your receiveNotification method, you can refer to your own method, which implements code to check wether you are in the app foreground or not.
By checking the [[UIApplication sharedApplication] state]; I think.
Then, next to your last queston again.
When your app is closed, and you send a PN, (which you know, can have a dictionary of some bytes sended with it). and you click VIEW (you can even send a soundname with it, if it's within your apps bundle, it will play, this is all in the documentation).
So, if you click VIEW, the app will open itself, containing a dictionary in the didReceiveNotification stuff. And you can do stuff based upon that dictionary.
Hope that was of any help.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
This is done by OS..
And you don't need the applicationDidBecomeActive method.
In your receiveNotification method, you can refer to your own method, which implements code to check wether you are in the app foreground or not.
By checking the [[UIApplication sharedApplication] state]; I think.
Yes, that made things a bit more easy - my code was a little more convoluted. This is what I use now:

Code:
UIApplicationState state = [application applicationState];
if (  (state == UIApplicationStateBackground) || (state == UIApplicationStateInactive)  ) 
{
    // DO STUFF
}

Then, next to your last queston again.
When your app is closed, and you send a PN, (which you know, can have a dictionary of some bytes sended with it). and you click VIEW (you can even send a soundname with it, if it's within your apps bundle, it will play, this is all in the documentation).
So, if you click VIEW, the app will open itself, containing a dictionary in the didReceiveNotification stuff. And you can do stuff based upon that dictionary.
Hope that was of any help.
Aha, found it! You need to check in the didFinishLaunchingWithOption for UIApplicationLaunchOptionsRemoteNotificationKey to get to the dictionary containing the PN-stuff, not in the didReceiveNotification.

Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
    NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    if( [apsInfo objectForKey:@"alert"] != NULL)
    {
        // REACT TO PN IF APP WAS CLOSED
    }
..
}
Thanks for your help, jnoxx :)