【发布时间】:2021-10-10 23:29:06
【问题描述】:
我想知道如何检测我的应用是否是通过用户点击本地通知启动的。
我目前正在使用方法userNotificationCenter(_:didReceive:withCompletionHandler:) 我的应用程序正确检测到应用程序是从使用此方法的通知中启动的,仅当应用程序仍处于活动状态时。如果我通过在应用切换器中向上滑动来结束应用,然后点击通知会打开应用,但不会在 userNotificationCenter(_:didReceive:withCompletionHandler:) 中注册。
我之前在viewDidLoad() 中将UNUserNotificationCenter.current().delegate 设置为self。有人说改成application(_:didFinishLaunchingWithOptions:),我试过这样:
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
但是,问题仍然存在。
我的问题:我如何检测我的应用是否是通过用户点击本地通知启动的,该应用是处于活动状态还是非活动状态?我正在使用 swift 5 和 Xcode 13。
【问题讨论】: