我想安排 X 分钟的本地通知,并在用户执行操作时将用户带到指定的链接。
当前,当应用程序处于前台
或非事件
时,将调用委托(delegate)方法UNUserNotificationCenter(didReceive: withCompletionHandler:)
,并且应用程序的工作方式如下预期(深层链接打开)
我遇到的问题是,当应用程序暂停或在后台时收到通知,并且通知启动应用程序时,从我所看到的来看,我似乎无法捕获收到链接的位置,也无法跟踪该链接该委托(delegate)方法没有被调用?
下面是 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
条目时,它是空的。
最佳答案
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转到底部的处理远程通知
关于swift - 当应用程序处于非事件状态时,从本地通知打开深层链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58908719/