15

I want my app to do specific things when the app is launched by a click on a notification. I want to do these specific things when the app is already running into background BUT ALSO when the app is started FROM SCRATCH (not running into background) by a click on the notification.

When the app is started from background by a click on the notification, I get the notification via:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

=> NO PROBLEM !

When the app is started from scratch by a click on the notification, I would like to get the notification via:

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

     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

}

But launchOptions is always nil !! It is nil when the app is started from scratch via a click on the app icon (normal) but also when the app is started from scratch via a click on a notification (not normal).

Anybody knows how to solve this issue ?

Thanks !!!

EDIT 1

Here is how my notifications are created (Joe question):

  NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:notifText,latitudeString,longitudeString,nil]
forKeys:[NSArray arrayWithObjects:@"notifText",@"latitude",@"longitude", nil]];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody =msg;
    localNotif.alertAction = @"Ok";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.userInfo = userInfo;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

EDIT 2 (ANSWER TO MY QUESTION! :))

Here is the procedure I used to debug my app but...this procedure is wrong!!

  1. I set up my debugger with the "Wait for MyApp.app to launch" option in the "Edit Scheme" menu
  2. I launched my app with XCode a first time (launch from scratch) XCode displays the "Waiting for my MyApp to launch" => I CLICKED ON MY APP ICON to launch the app
  3. The app is launched => I clicked on the home button => the notification is displayed
  4. I clicked on the stop button in XCode to close the app I relaunched it with XCode => XCode displays again the "Waiting for my MyApp to launch" message => I CLICKED ON THE NOTIFICATION in the status bar to launch the app
  5. => launchOptions is nil !

launchOptions equal to nil is due to the fact that relaunching the app with XCode (in this case with the "Waiting for my MyApp to launch" option) deletes the notifications even if it is still displayed in the status bar...

To be able to debug check what is the content of launchOptions after a relaunch of the app from scratch by a click on a notification, it seems that the only way is to display this content in a UIAlert as mentioned in answer by Tammo Freese. So, use the following to debug in this specific case:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    return YES;
}

Thanks all for your help !!!!

toto_tata
  • 14,526
  • 27
  • 108
  • 198
  • Can you add your notification payload to your question? – Joe Nov 15 '12 at 11:54
  • Thanks for your answer Joe. I added how my notifications are created in my question. Hope this will help you to help me :) – toto_tata Nov 15 '12 at 13:53
  • Hi @Regis_AG, I could get local notifications to work even when using "Wait for App to launch" – see the edit of my answer below. – Tammo Freese Nov 26 '12 at 16:21
  • I don't know if it can have an influence but I use XCode 4.5. Do you use this version or an earlier version? Note that I launched the app both times (step1 of your edit and step4 of your edit) with the "Wait for App to launch" option (step1: click on the icon to link the debugger and launch ; step4: click on the notif to link and launch). Does it work in your case if you use the "Wait for App to launch" option in both cases ? – toto_tata Nov 28 '12 at 08:16

4 Answers4

19

I could not find an error in the code you shared. Normally this should work both in the simulator, and on the device. Here is an example that worked for me: First generate a new Single View iPhone app (ARC and Storyboards on). Then change two methods in the AppDelegate as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = @"Just some text";
    localNotif.alertAction = @"OK";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.userInfo = @{@"test": @YES};

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Then start this app, press the home button, then stop the app. If you tap on the local notification which comes in 10 seconds after pressing the home button, you should see something like this, which shows the local notification has been passed to -application:didFinishLaunchingWithOptions::

screenshot of the iPhone Simulator showing an alert view that displays a local notification

My advice is: First get the example I posted above to work to make sure nothing has gone awry with your setup, then check what you are doing differently in your code.

Edit

This applies to Edit 2 of the question: Local notifications also seem to work for me when waiting for the app to launch (in the simulator, not on the device). Try this:

  1. Install the sample app described above and launch it with "Wait for MyApp.app to launch" disabled.
  2. Click on the home button, then stop the app via Xcode or via the task bar.
  3. Enable "Wait for MyApp.app to launch".
  4. If you now tap the notification in the notification center, it is shown in the alert view.
Tammo Freese
  • 10,514
  • 2
  • 35
  • 44
  • Thanks for your answer Tammo! Thanks to your idea to display an UIAlert to see the content of the notification at startup, I found why I did wrong! :) The problem is that I set up my debugger with the "Wait for MyApp to launch" in the "Edit Scheme" menu. I did that in order to be able to debug my app when launching it from scratch after a click on the notification but...this procedure deletes all the notifications at startup! I am going to edit my question to explain that more clearly. Thanks a lot for your help!! – toto_tata Nov 26 '12 at 10:49
1

Dont know if this is what your looking for, but are you missing 'return YES;'? Because i use this to perform a segue to a new view from a notification and it works fine

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigation = (UINavigationController *) self.window.rootViewController;

    [navigation.visibleViewController performSegueWithIdentifier:@"Ident" sender:nil];

    return YES;
}
agierens
  • 132
  • 9
  • Thanks for your answer. Actually, the YES was in my code (I just for got to copy it in my question). The problem was finally due to the way I debugged. See my answer. Thanks for your help anyway. – toto_tata Nov 26 '12 at 11:17
1

My conclusion and suggestion if it can hep anyone, refer below

Every time you have new build to test your app, you must test the notification click actions with the notifications generated by latest app. If you keep on testing of click actions with old notifications generated by older build then It will behave unexpectedly (means somehow its able to launch the app but it will not return you any valid info in didFinishLaunchingWithOptions:)

iDevAmit
  • 1,550
  • 2
  • 21
  • 33
0

Please forgive me, I am still new when it comes to Apple's Push Notification service, but I did some reading through their documentation and my guess is that there may be something in the creation of your local notification that is causing the problem.

I would double check how you are creating your local notification with Apple's Example as shown here in Listing 2-1 just to rule out that possibility. Since some of the code that is used for creating your local notification isn't displayed to us in this thread, it makes it difficult to assess if the instantiation of your local notification is indeed correct. It could end up being as simple as something being wrong with the fire date or something else in the notification not being set up correctly.

Anil
  • 2,539
  • 6
  • 33
  • 42
  • Thanks for your answer. It was not a question of notification instantiation but the problem was due to the way I debugged. See my answer. Thanks for your help anyway. – toto_tata Nov 26 '12 at 11:16
  • You are welcome. Happy to hear you were able to resolve the issue. Cheers. – Anil Nov 26 '12 at 15:07