I'm doing this app that needs to send notifications when an event is coming.
Everything is working but when the app is closed and i open through the notification it doesn't fire how it is supposed to.
My App Delegate didFinishLaunchingWithOptions and didReceiveLocalNotification
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
createCopyOfDatabaseIfNeeded()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound |
UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
if let options = launchOptions {
let value = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
if let notification = value {
self.application(application, didReceiveLocalNotification: notification)
}
}
return true
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Do something serious in a real app.
println("Received Local Notification:")
println(notification.alertBody)
if notification.alertAction == "editList" {
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
}
}
In my main tab controller:
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleModifyListNotification", name: "modifyListNotification", object: nil)
super.viewDidLoad()
self.repaint()
}
func handleModifyListNotification(){
dispatch_async(dispatch_get_main_queue(), {
// Show the alert
})
}
The main objective is when a notification is pressed with the app closed, it opens, executes didFinishLaunchingWithOptions and checks for UILocalNotification in launchOptions and calls didReceiveLocalNotification.
ps: i know with debug that the notifications is being well received, the system is just not calling the didReceiveLocalNotification method, like it should.
1º Edit
I'm really calling the didReceiveLocalNotifaction method because i just tried this and it worked
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Do something serious in a real app.
println("Received Local Notification:")
println(notification.alertBody)
if notification.alertAction == "editList" {
var xpto = UIAlertView()
xpto.title = notification.alertAction!
xpto.show() NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
}
}
So i gess this must be a timing issue. I really want my tab controller to react to this notification.
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
If I'm not mistaken, -didReceiveLocalNotification: is only called when the app is in active state and being used.
If you're attempting to launch the app from a background state, I would suggest using -didFinishLaunchingWithOptions: and querying your options.
For this reason, maybe have your notification handling code in a separate method and have it called from both -didReceiveLocalNotification & -didFinishLaunchingWithOptions ?