ankakusu
ankakusu

Reputation: 1828

Handling Apple Push Notification Service

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?

Upvotes: 3

Views: 529

Answers (4)

Eran
Eran

Reputation: 393771

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.

Upvotes: 3

Rok Jarc
Rok Jarc

Reputation: 18865

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.

Upvotes: 2

Groot
Groot

Reputation: 14251

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!

Upvotes: 0

seriakillaz
seriakillaz

Reputation: 843

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

Upvotes: 0

Related Questions