React to Push Notification when app is closed

Discussion in 'iOS Programming' started by Danneman101, Jan 29, 2012.

  1. Danneman101, Jan 29, 2012
    Last edited: Jan 29, 2012

    Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #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?
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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 ;)
     
  3. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    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?
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    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.
     
  5. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #5
    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
    }
    

    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 :)
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6

Share This Page