我想安排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
条目时,它是空的。
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
将使用推送有效负载被调用。
苹果链接到底部的Handling Remote Notifications