Search code examples
iosobjective-cuilocalnotification

AppDelegate didFinishLaunchingWithOptions launchOptions always returning (null)


I'm trying to get a scheduled UILocalNotification with the app fully closed (terminated), since the method didReceiveLocalNotification: doesn't get called, I'm trying to use the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(@"LOCAL NOTIFICATION - %@",localNotification);
if (localNotification) {
    //HANDLE THE NOTIFICATION
}
return YES;

}

That is the way I'm creating the UILocalNotification :

UILocalNotification * notificationRH = [UILocalNotification new];
notificationRH.fireDate = date;
notificationRH.repeatInterval = repetition;
notificationRH.alertBody = body;
notificationRH.alertAction = title;
notificationRH.hasAction = title ? 1 : 0;
notificationRH.timeZone = [NSTimeZone defaultTimeZone];
notificationRH.soundName = sounds ? UILocalNotificationDefaultSoundName : nil;
notificationRH.userInfo = @{@"test": title};

But the problem is that the launchOptions on the method always return (null), and I'm not able to get the Notification triggered while the app was closed. Does anyone can help me with this ?? Thanks a lot!!


Solution

  • The problem is that you have forgotten to schedule the local notification:

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/scheduleLocalNotification: