The app that I have made keeps track of Goals and Milestones(which belong to a goal) and reminds the user when they are about to reach the due date of a goal or a milestone through a UILocalNotification
my app has been successful in handling local notifications when the app is in a background state and when the app is in the foreground.
I am aware that if you want to handle local notifications when they are received when the app is terminated you are required to access the local notification through the launch options of the application:didFinishLaunchingWithOptions: method in your appDelegate.
However whenever I test the app and activate the local notification after the app has terminated, I am unable to handle the local notification in application:didFinishLaunchingWithOptions:
I suspect it has something to do with either launch options being nil or launch options not having the local notification payload.
The way I test this scenario is as such:
- I build and run the app (command + R)
- I schedule the notification
- I stop running the simulator and then change my mac's time to just before the fire date
- I build and run the app again and then proceed to terminate the app from the simulator (by shift + command + h twice and then swiping up)
- I lock the screen and wait for the notification
- After it fires I swipe the notification on the lock screen
After the notification fires and i swipe the notification, the app is launched but the method that i used in handling the local notification payload in application:didFinishLaunchingWithOptions: is not called whatsoever.
My Code in my application:didFinishLaunchingWithOptions: to handle the local notification payload is as follows:
if let options = launchOptions {
let value = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
if let notification = value {
self.application(application, didReceiveLocalNotification: notification)
}
}
My Code in my application:didReceiveLocalNotification: is as follows:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
//If the user receives a notification while app is in the foreground
println("")
if application.applicationState == UIApplicationState.Active {
println("did receive notification")
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
let alertController = UIAlertController(title: nil, message: notification.alertBody!, preferredStyle: UIAlertControllerStyle.Alert)
let alertAction = UIAlertAction(title: "View", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
self.performFollowUpActionForNotification(notification)
}
let cancelAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(alertAction)
alertController.addAction(cancelAction)
self.window!.rootViewController!.presentViewController(alertController, animated: true, completion: nil)
}
else {
performFollowUpActionForNotification(notification)
application.applicationIconBadgeNumber = 0
}
}
as well as the helper method performFollowUpActionForNotification:
func performFollowUpActionForNotification(notification:UILocalNotification) {
if notification.alertAction! == "View Goal" {
if let tabVC = self.window?.rootViewController? as? UITabBarController {
let navCon = tabVC.viewControllers![0] as UINavigationController
if navCon.topViewController is GoalPageViewController {
}
else {
navCon.pushViewController(navCon.viewControllers![0] as UIViewController, animated: true )
}
}
}
if notification.alertAction! == "View Milestone" {
if let tabVC = self.window?.rootViewController? as? UITabBarController {
let navCon = tabVC.viewControllers![0] as UINavigationController
if let goalPageVC = navCon.viewControllers![0] as? GoalPageViewController {
goalPageVC.findGoalThatContainsMilestoneAndGoToDetailForNotification(notification)
}
else {println("cant down cast view controller to goal page view controller")}
}
}
}
Is the problem with my code or how i test my app for this scenario? I am desperately in need of an answer.