[英]Checking if iOS app was launched from local notification?
如何检测是否从本地通知中启动了未处于活动,非活动或后台状态(已终止)的应用程序? 到目前为止,我已经在App Delegate的didFinishLaunchingWithOptions中尝试了两种方法:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// METHOD 1:
if let options = launchOptions {
if let key = options[UIApplicationLaunchOptionsLocalNotificationKey] {
notificationCenter.postNotification(NSNotification(name: "applicationLaunchedFromNotification", object: nil))
}
}
// METHOD 2:
let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
if (notification != nil) {
notificationCenter.postNotification(NSNotification(name: "applicationLaunchedFromNotification", object: nil))
}
return true
}
在我的View Controller中,我观察ViewDidLoad中的通知,并作为响应设置UILabel的文本:
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: "handleAppLaunchFromNotification", name: "applicationLaunchedFromNotification", object: nil)
}
func handleAppLaunchFromNotification() {
debugLabel.text = "app launched from notification"
}
但是从本地通知启动终止的应用程序后,再也不会设置UILabel的文本。
我的问题是:
print()
。 您在didFinishLaunchingWithOptions
中检查本地通知,此方法仅包含远程通知的launchOptions
。 如果应用程序处于终止状态,并且您对本地通知执行操作,则didFinishLaunchingWithOptions
方法之后, didReceiveLocalNotification
将被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.