I have implemented UILocalNotifications
in two different apps now. One uses Location Services, which means if it gets killed, it (usually) gets restarted (so it's not as big of an issue). However, in another app, I schedule a UILocalNotification
based upon time. In this case, I'm having a major problem.
If I schedule a notification, then the app gets killed, pushed out of memory, phone is turned off then on again, I cannot view older notifications "automatically" when opening the app.
Here is the workflow that works (app running in the background):
Here is the workflow that does not work (app no longer running in the background):
didReceiveLocalNotification
method is not called. The user thinks the app doesn't work.Is there any way to know which notification they touched on when the app is not running in the background (and thus does not run the didReceiveLocalNotification method
)?
In the 2nd case, the user has quit (or killed) the app, so when the user touches the notification your app would be launched.
This can be handled in the below mentioned appDelegate's method
didFinishLaunchingWithOptions
is invoked when an app is launched, so just check if the app was actually launched because the user touched the notification (code below):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSLog(@"notification caused app to launch, alert body = %@", notification.alertBody);
// do what ever action you want to do
// you could just copy the code from "didReceiveLocalNotification" and paste it here
}
return YES;
}
@Ranjit, this will work when user tap the app icon because is an app delegate method. Get called every time the app finished launching. (Can´t comment because my lack of reputation... ;) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With