swift - 当应用程序处于非事件状态时,从本地通知打开深层链接

标签 swift uilocalnotification deep-linking

我想安排 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/

相关文章:

ios - 如果时间过去,UILocalnotification 会直接触发

ios - 打开和关闭闹钟 ios

从深度链接启动时出现 iOS 错误 : lsd[738] <Warning>: LaunchServices: application launch failed - timeout waiting for launch

android - Facebook 深度链接在 Android 中无法使用

iphone - ScheduleLocalNotification 不适用于越狱应用程序?

ios - 如何从 React Native 应用程序打开 iOS 画廊应用程序

swift - 在 Swift 3 中使用 KVO 检查值是否被更改

ios - AudioKit/AVAudioEngine 启动时间过长

iphone - 如何使用 swift 验证本地播放器?

ios - 无法识别的选择器发送到实例 0x103026600 payfort