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
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.
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:)
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:
:
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:
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;
}