3

I'm using apple push notification service in my project.

Please follow the 2 ways of opening the app and handling this push notifications. In the second scenario I do not know how to handle it. Do you know how?

The push notification arrived to my device,

Scenario 1:

  1. I clicked on the push notification.

  2. The - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo function AppDelegate.m file catches this function.

Scenario 2:

  1. I normally opened the device (by clicking on the app)

  2. How can I handle the push notification?

Eran
  • 387,369
  • 54
  • 702
  • 768
ankakusu
  • 1,738
  • 8
  • 26
  • 37
  • 1
    it looks like Scenario 2 is not a problem. usually, when a user launches an app by touching the app icon, this means he does not wish to trigger an action from the push notification. by the way, if you want to implement push notification easily, you can check out the following SDK by PushApps http://www.pushapps.mobi/ – Raz Dec 25 '13 at 15:56

4 Answers4

3

The other answers show how to get the notification data when the user taps the notification.

The difference between the two nethods shown is that one is called when app is already running, either in foreground or background, while the other is called when app is not running at all.

On your second case, when the user doesn't tap the notification, the notification data isn't passed to the app when you open it with the launch Icon.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Do you suggest me a pattern to handle this newly coming notification for this case (app opened with the launch icon)? – ankakusu Dec 26 '13 at 12:10
2

First scenario:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSLog (@"APNS: notification received: %@", userInfo);

    NSString *message = nil;
    id alert = [userInfo objectForKey:@"aps"];

    if ([alert isKindOfClass:[NSString class]])
    {
        message = alert;
    }
    else if ([alert isKindOfClass:[NSDictionary class]])
    {
        message = [alert objectForKey:@"alert"];
    }

    if (message)
    {
        if (![message isEqualToString:@""])
        {
            UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle: @"notification"
                                      message: message
                                      delegate: nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
            [alertView show];
        }
    }
}

Second scenario:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog (@"LAUNCH OPTIONS: %@",launchOptions);

    id remoteNotificationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotificationValue)
    {
        NSString *message = nil;
        id alert = [remoteNotificationValue objectForKey:@"aps"];

        if ([alert isKindOfClass:[NSString class]])
        {
            message = alert;
        }
        else if ([alert isKindOfClass:[NSDictionary class]])
        {
            message = [alert objectForKey:@"alert"];
        }

        if (message)
        {         
            if (![message isEqualToString:@""])
            {
                UIAlertView *alertView = [[UIAlertView alloc]
                                          initWithTitle: @"notification"
                                          message: message
                                          delegate: nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
                [alertView show];
            }
        }
    }
    ....

Of course you might want to make a special method that handles notifications and is called from both scenarios (with NSDictionary * parameter) so your code would be more readable. Sometimes APNS notifications are useful also when app is running - empty notification (with no payload) might be used to trigger the data synchronization with server to avoid polling for example.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
0

You can get the arrived notifications when the app starts with the following code (e.g: in application:didFinishLaunchingWithOptions):

NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

Here is a more thorough explanation: How to manage notification when users click on badge

Community
  • 1
  • 1
seriakillaz
  • 833
  • 12
  • 22
0

You can handle that like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Checking if app was launched from the notification
    if (launchOptions != nil) {

        NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

        if (dictionary != nil){
            // Read dictionary and do something since the app
            // was launched from the notification.
        }

    }

Here is an example of what the dictionary object contains

NSString *message = @"";
NSString *badge = @"";
NSString *sound = @"";

if([dictionary objectForKey:@"alert"]) {
    message = [dictionary objectForKey:@"alert"]; 
}

if([dictionary objectForKey:@"badge"]) {
    badge = [dictionary objectForKey:@"badge"]; 
}


if([dictionary objectForKey:@"sound"]) {
    sound = [dictionary objectForKey:@"sound"]; 
}

Hope it helps!

Groot
  • 13,943
  • 6
  • 61
  • 72