【问题标题】:open to deep link from local notification when app is inactive当应用程序处于非活动状态时,从本地通知打开深层链接
【发布时间】:2019-11-18 05:58:05
【问题描述】:

我想安排 X 分钟的本地通知,并在操作时将用户带到指定的链接。

当前,当应用程序位于 foregroundinactive 时,会调用委托方法 UNUserNotificationCenter(didReceive: withCompletionHandler:) 并且应用程序按预期工作(深层链接打开)

我遇到的问题是当应用程序暂停或后台接收通知并且通知启动应用程序时,我似乎无法捕获收到链接的位置并且无法跟踪链接,据我所见那个委托方法没有被调用?

下面是UNUserNotificationCenter(didReceive: withCompletionHandler:)的实现

defer {
    completionHandler()
}

guard
    response.actionIdentifier == UNNotificationDefaultActionIdentifier ||
    response.actionIdentifier == "open-dl" else {
    return
}

guard
    let url = response.notification.request.content.userInfo["link-to"] as? String,
    let linkTo = URL(string: url) else {
    return
}

if UIApplication.shared.applicationState == .background {
    UserDefaults.standard.set(linkTo, forKey: "localdeeplink")
    UserDefaults.standard.synchronize()
} else {
    _ = UIApplication.shared.delegate?.application?(UIApplication.shared, open: linkTo, options: [:])
}

当我尝试从UserDefaults 中读取localdeeplink 条目时,它是空的。

【问题讨论】:

  • 当您遇到此问题时,UNUserNotificationCenter(didReceive: withCompletionHandler:) 被调用?
  • 当我从挂起,后台 -> 接收通知 -> 点击通知 - 我不确定是否调用了该函数,我假设它没有被调用,因为 Userdefaults 条目是未设置。
  • 这个函数被调用了?打印一些要知道的东西,我认为它没有被调用
  • 我无法打印任何东西,因为要测试我需要终止应用程序并接收通知,但未附加调试器
  • 如果它确实可以帮助您解决问题,您可以投票并接受答案。

标签: swift uilocalnotification deep-linking


【解决方案1】:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    //add this function
    if launchOptions != nil {
        pushAction(launchOptions: launchOptions)
    }
    return true
}

func pushAction(launchOptions: [NSObject: AnyObject]?) {
    //add code of UNUserNotificationCenter(didReceive: withCompletionHandler:) here
}

pushAction函数中添加UNUserNotificationCenter(didReceive: withCompletionHandler:)的代码

当应用程序关闭时,didRecive 没有被调用,didFinish 使用推送负载调用。

Apple link 转到底部的Handling Remote Notifications

【讨论】:

  • 好的,我如何从启动选项中拉出本地通知?这样做的关键已弃用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多