【问题标题】:iOS 13+: Can't get a notification userInfo when opening a deeplinkiOS 13+:打开深层链接时无法获得通知 userInfo
【发布时间】:2021-09-24 14:34:56
【问题描述】:

我遇到了通知和深层链接无法解决的问题。

当打开一个深度链接时,UIApplication 用于发送带有 userInfoUIApplication.didFinishLaunchingNotification,其中包含带有打开的 URL 的 UIApplicationLaunchOptionsURLKey

所以我们可以这样订阅它:

NotificationCenter.default.addObserver(forName: UIApplication.didFinishLaunchingNotification, object: nil, queue: nil) { (notification) in
    print(notification.userInfo) // prints UIApplicationLaunchOptionsURLKey: url_that_opened_the_app
}

现在,在 iOS 13 之后不会发生这种情况:userInfonil

所以问题是:有没有办法在应用打开深层链接时从通知中心接收通知?

*想法:* 我认为这是因为UISceneDelegate 现在负责打开深度链接,这可以通过以下事实得到证实:如果我们删除 SceneDelegate,我们可以取回我们的 userInfo。 我试图查找是否有任何 SceneDelegate 通知向我们提供此类信息,但它们没有:didActivateNotificationwillConnectNotification 都没有提供任何信息。

【问题讨论】:

  • 检查我的答案一次..

标签: ios iphone deep-linking nsnotificationcenter nsnotifications


【解决方案1】:

是的,您也可以使用 SceneDelegate 找到相同的内容。
这是我制作的Sample

从深层链接启动应用后,您将收到connectionOptions in

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
}

您可以通过以下方式获取您的网址:

if let context = connectionOptions.urlContexts.first as? UIOpenURLContext {
    print(context.url.absoluteString)
}

所以你的函数看起来像:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let _ = (scene as? UIWindowScene) else { return }
    
    if let context = connectionOptions.urlContexts.first {
        print(context.url.absoluteString)
        //Here you can pass this URL to your notification and perform your logic.
    }
}

我测试的 DeepLink 是:
deeplinks://mycustomDeepLink

我还将方案更改为Wait for the Executable to Launch,以检查来自深层链接的初始启动内容。

注意:有时调试器出于某种原因无法工作。因此,在我的示例中,我在 ViewController 中添加了一个警报视图。

【讨论】:

  • 嗨拉詹!感谢您的回答,但这不是我一直在寻找的:我想知道我们是否可以收到包含此数据的通知(来自通知中心)
  • @LeonidSilver 您可以通过创建数据的通用结构并发布通知(不管SceneDelegate 是否存在)来触发自己的通知,而不是依赖UIApplication.didFinishLaunchingNotification,因为这些代表仅在应用程序启动时调用。因此,您确实确定您的特定通知仅在应用程序启动时触发并且它具有深层链接 url
  • 我正在制作一个无法访问 AppDelegate 的 SDK,因此我看到的唯一选择是依赖 Apple 通知。
猜你喜欢
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
相关资源
最近更新 更多