无法从通知操作处理程序打开URL

ewerx

我正在尝试通过在iOS 10 / Swift 3上打开URL来处理本地通知。我为通知的默认操作分配了一个URL,为自定义操作按钮分配了另一个URL。

触发默认操作(点击通知本身)后,该应用程序将成功打开URL。但是,当轻按操作按钮时,将UNUserNotificationCenterDelegate调用处理程序并canOpenURL返回true,但是打开URL的调用失败。

仅当应用程序在后台或在点击通知时终止时,才会发生此问题。如果应用程序在前台,则两种情况都可以。

我的应用程序委托application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:])方法中也有一个断点它对默认操作有效,但对自定义操作无效。

处理自定义操作时,我需要做些不同的事情吗?

这是代码:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
    switch response.actionIdentifier {
    case UNNotificationDefaultActionIdentifier:
        if let link = URL(string: "myapp://default"),
                    UIApplication.shared.canOpenURL(link) {
                    UIApplication.shared.open(link) { result in
                        print("open url result: \(result)") // this works!
            }
        }

    case "customActionIdentifier":
        if let link = URL(string: "myapp://custom"),
                    UIApplication.shared.canOpenURL(link) {
                    UIApplication.shared.open(link) { result in
                        print("open url result: \(result)") // this fails!
            }
        }
    }

    completionHandler()
}
ewerx

我终于找到了问题:声明UNNotificationAction时,必须在options数组中包括UNNotificationActionOptions.foreground。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章