检测App是否通过点击本地通知Swift启动



我想知道如何检测我的应用程序是否从用户点击本地通知启动。

我目前正在使用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版本:SwiftXcode版本:Xcode 13

查看options中的场景委托willConnect方法或(如果没有场景委托)您的应用程序委托willFinishLaunching方法。这将告诉你你是如何启动的。

最新更新