应用关闭时点击 UILocalNotifications
Clicking in UILocalNotifications when the app is closed
我正在做这个需要在事件到来时发送通知的应用程序。
一切正常,但是当应用程序关闭并且我通过通知打开它时,它不会按预期方式触发。
我的 App Delegate didFinishLaunchingWithOptions 和 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);
}
}
在我的主选项卡控制器中:
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
})
}
主要的 objective 是在关闭应用程序的情况下按下通知时,它会打开,执行 didFinishLaunchingWithOptions 并检查 launchOptions 中的 UILocalNotification 并调用 didReceiveLocalNotification。
ps:我通过调试知道通知被很好地接收,系统只是没有调用 didReceiveLocalNotification 方法,就像它应该的那样。
1º 编辑
我真的在调用 didReceiveLocalNotifaction 方法,因为我刚刚尝试了这个并且它起作用了
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);
}
}
所以我想这一定是时间问题。我真的希望我的选项卡控制器对此通知做出反应。
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
如果我没记错的话,-didReceiveLocalNotification: 仅在应用程序处于活动状态并被使用时调用。
如果您尝试从后台状态启动应用程序,我建议您使用 -didFinishLaunchingWithOptions: 并查询您的选项。
出于这个原因,也许将您的通知处理代码放在一个单独的方法中,并从 -didReceiveLocalNotification 和 -didFinishLaunchingWithOptions 中调用它?
我正在做这个需要在事件到来时发送通知的应用程序。
一切正常,但是当应用程序关闭并且我通过通知打开它时,它不会按预期方式触发。
我的 App Delegate didFinishLaunchingWithOptions 和 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);
}
}
在我的主选项卡控制器中:
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
})
}
主要的 objective 是在关闭应用程序的情况下按下通知时,它会打开,执行 didFinishLaunchingWithOptions 并检查 launchOptions 中的 UILocalNotification 并调用 didReceiveLocalNotification。
ps:我通过调试知道通知被很好地接收,系统只是没有调用 didReceiveLocalNotification 方法,就像它应该的那样。
1º 编辑
我真的在调用 didReceiveLocalNotifaction 方法,因为我刚刚尝试了这个并且它起作用了
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);
}
}
所以我想这一定是时间问题。我真的希望我的选项卡控制器对此通知做出反应。
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil);
如果我没记错的话,-didReceiveLocalNotification: 仅在应用程序处于活动状态并被使用时调用。
如果您尝试从后台状态启动应用程序,我建议您使用 -didFinishLaunchingWithOptions: 并查询您的选项。
出于这个原因,也许将您的通知处理代码放在一个单独的方法中,并从 -didReceiveLocalNotification 和 -didFinishLaunchingWithOptions 中调用它?