【发布时间】:2019-11-18 05:58:05
【问题描述】:
我想安排 X 分钟的本地通知,并在操作时将用户带到指定的链接。
当前,当应用程序位于 foreground 或 inactive 时,会调用委托方法 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