I have an iOS application which uses local notifications. When the app is running in the background or is active, I use the application didReceiveLocalNotification
methods and that works great. But when the app is closed (not running in the background), I use the application didFinishLaunchingWithOptions
method to control what happens when the notification is handled.
However, the problem I have is that the notification is always (null)
in the didFinishLaunchingWithOptions
method.
Here is my code:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Register the app for local notifcations.
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
// Setup the local notification check.
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
//UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:notification delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
// Check if a notifcation has been received.
if (notification) {
// Run the notifcation.
[self.myViewController run_notifcation:[notification.userInfo valueForKey:@"notification_id"]];
}
// Ensure the notifcation badge number is hidden.
application.applicationIconBadgeNumber = 0;
return YES;
}
Am I missing something here? I have asked the user for permission first and have the correct code to get the notification object. My app is built for iOS 9 and higher only.
Thanks for your time, Dan.