2

I am working on one iPhone app which involves a push notification. As I have seen in many documents and tutorials it suggests to register for push notification with in

application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

like the following:

- (BOOL)application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge|  UIRemoteNotificationTypeSound]; 

    ...
} 

now the question is, if the app was not running (even in background), when the push come, it cant process the push message, but if I use the push message again from the notification area and lunch the app again, I can get my message.

what I need to do to make my app get the push message even when it lunch for the first time?

Cœur
  • 37,241
  • 25
  • 195
  • 267
shebelaw
  • 3,992
  • 6
  • 35
  • 48
  • How are you going to send a push to the device before you've registered, got the key & sent it to your server, though? – mattjgalloway Feb 07 '12 at 21:39
  • if the app was used in the past my server will keep track of the device tokens, so I can send a push even though it is not ruining at that time. But now the issue is when the app launch first time. it is trying to sign up for notification not to process a notification. how I can solve this problem? – shebelaw Feb 07 '12 at 21:42
  • Hang on what? I think you've confused things. See @warrenm's answer. – mattjgalloway Feb 07 '12 at 21:53
  • Hang on? please read my question properly. – shebelaw Feb 07 '12 at 22:53

1 Answers1

7

You might be conflating the notion of registering for and receiving notifications. It is impossible for an app to receive a push notification before the registerForRemoteNotificationTypes: method is called the first time, since this method provides the push token that is used to send push notifications in the first place.

So, you must be talking about receiving notifications under the two separate situations in which they can be delivered: upon initial app launch, and during the execution of the program.

In order to handle notifications of the first type, you must inspect the options dictionary sent to application:didFinishLaunchingWithOptions:. The following code shows how to route a notification received at launch to the delegate method that is called when a push notification arrives while the app is already running.

Place this in your application:didFinishLaunchingWithOptions: override:

NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
    [self application:application didReceiveRemoteNotification:pushNotificationPayload];
}
warrenm
  • 31,094
  • 6
  • 92
  • 116
  • yes, this is probably the right solution, I will test it. Thanks Warrenm – shebelaw Feb 07 '12 at 21:45
  • Yes this is the right solution but what if I want alert (`UIAlert` placed inside `didReceiveRemoteNotification` method) will appear when user will click on App Icon, rather than push notification! – Vaibhav Saran May 02 '13 at 09:42
  • You might have better luck posting this as a new question rather than as a comment on an answer that's more than a year old. – warrenm May 03 '13 at 03:24