I'm working with UILocalNotification
and I would like to notify one of my controller that the notification has been received even if the app has been terminated.
In my appDelegate I implemented this function:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:notification.userInfo];
}
}
In my UIViewController
I implemented the observer on the viewDidLoad
method
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didlocalNotificationReceived:) name:@"localNotificationReceived" object:nil];
}
It works perfectly when the app run in background. Since the viewDidLoad
method has already been called and the Observer is waiting..
The issue is when I kill the app. Then the observer of my controller is gone and the didlocalNotificationReceived
method is never called.
I think that it's because when I receive the localNotification
and run the app again. The didReceiveLocalNotification:
method is called before the viewDidLoad
of my UIViewController
. Then the observer is created after the PostNotificationName
then the observer receives nothing.
I would like to know if there is some best practices or pattern to handle this kind of issue.
I know that the didFinishLaunchingWithOptions
method is called before didlocalNotificationReceived
so there is probably something to do there.
UPDATE :
I also discovered that when is app is terminated. Once you tap the notification, It opens the app, call the function didFinishLaunchingWithOptions
but never call didReceiveLocalNotification
. So I think that I'm gonna handle both cases differently.