Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got PN however application:application didReceiveRemoteNotification: not triggered when app icon is selected

I'm seeing a peculiar behavior with my push notification and was wondering if anyone has any advise on what I did wrong or should do.

I have my application:(UIApplication*)application didReceiveRemoteNotification: written as follows:

- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
{
NSLog(@"Received notification: %@", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
} 

As you can see, I'm not worried about the state of the app. I just want to log a message whenever I get a PN.

My setup is as per the documentation from Apple and I can receive push notifications.

The following are the expected behaviours when a PN comes in:

  • App is in the background and I click on the notification: I can see the notification in notification center. Click on the notification, the app comes to the foreground and I can see the above method is called.
  • App is active and already in the foreground: I can see the above method is called.

Now, the following is the peculiar behaviour I am seeing:

  • PN comes in. See it in the notifications, see the badge on the app icon. I click on the app icon - not the notification: In this instance I found that the above method is not called. I was expecting it to be called...

Has anyone seen this behaviour before? Is this what should happen? I couldn't see anything in the Apple documentation regarding this... Also, would there be a way around this?

like image 857
Jimbalaya Avatar asked May 17 '12 00:05

Jimbalaya


2 Answers

If the app was not running in background, but initially launched from the push notification and you have didFinishLaunchingWithOptions: implemented, you need to implement your code there:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    if (launchOptions != nil) {
        NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
        if (userInfo != nil) {
            NSDictionary* apsInfo = [userInfo objectForKey:@"aps"];
            NSString* custom = [userInfo objectForKey:@"yourCustomPushData"];
            // do something with it
        }
    }
    //...
}
like image 102
MrJ Avatar answered Oct 27 '22 03:10

MrJ


I have the same behaviour. It drives me nuts but I think that's the way iOS works.

Below an excerpt from the apple documentation. This is regarding the application:didFinishLaunchingWithOptions when the app is not running. It looks like it's the same for when the app is in background/didReceiveRemoteNotification.

"If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . " From: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

like image 22
Oskar Avatar answered Oct 27 '22 01:10

Oskar