【问题标题】:Detect if App was Launched From Tapping Local Notification Swift检测应用程序是否通过点击本地通知 Swift 启动
【发布时间】: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。

【问题讨论】:

    标签: ios swift xcode swift5


    【解决方案1】:

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

    【讨论】:

    • 说“willConnect 方法”是指scene(_:willConnectTo:options:) 方法吗?
    • 是的。如果您有一个场景委托,该方法会在启动时调用,如果您是由与通知交互的用户启动的,options 对象将包含非零 notificationResponse
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2018-03-31
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多