【问题标题】:My URL isn't running after I tap on my notification action. What do I do?点击通知操作后,我的 URL 没有运行。我该怎么办?
【发布时间】:2017-02-09 21:19:27
【问题描述】:

在我的应用程序中,我希望在我的 通知 操作被点击时运行 URL。问题是,当我运行 app 时,它会将我置于前台,但不会运行 URL(我在 notification 操作中添加了 .foreground)。这是我的代码:

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.actionIdentifier == "call" {


           if let urled = URL(string: "tel://1234567891") {
                UIApplication.shared.open(urled, options: [:])                            
            }                

    }


}
}

任何人的帮助都会很棒:) 感谢您的任何回答!

【问题讨论】:

  • 变量number 在哪里设置或访问?我在任何地方都没有看到任何对它的引用。
  • @creeperspeak 变量number 设置在函数的正上方,我只是不想分享任何电话号码
  • 好的,您确认if let urled 正在通过吗?您可以尝试调用canOpenUrl 以查看该网址是否为有效格式。您是否尝试过打印"tel://\(number)" 以查看它是否正确?如果number 是可选的,您也需要先将其解包。可能有很多事情,但打印语句和断点可以为您解决问题。
  • @creeperspeak 通常我没有一个名为number 的变量,因为这个应用程序只是一个测试应用程序,通常我在那里有一个典型的字符串。我已经多次使用这个 url 进行了测试,它可以工作我只是不知道如何在点击通知操作时让它工作。
  • 试试canOpenUrl 只是为了确定,然后在if response... 处设置一个断点,然后逐行执行代码,看看会发生什么。

标签: ios swift url unusernotificationcenter usernotifications


【解决方案1】:

对于 iOS 10: 将 UserNotifications.framework 添加到您的应用中。 请检查您是否为推送通知设置了委托。 如果您已经设置了,请尝试 iOS 10 的以下方法。

import UserNotifications

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {
}



 func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
    print("Handle push from foreground")
    // custom code to handle push while app is in the foreground
    print("\(notification.request.content.userInfo)")

}


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

 print("Handle push from background or closed")

  // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background

print("\(response.notification.request.content.userInfo)")

}

【讨论】:

【解决方案2】:

试试这个:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

if response.actionIdentifier == "call" {

    if let urled = URL(string: "tel://\(1234567891)") {
        if UIApplication.shared.canOpenURL(urled) {
            UIApplication.shared.open(urled, options: [:], completionHandler: { (completed) in
                // completionHandler block
             }) 
         }
     } 
 }
completionHandler()}

【讨论】:

    【解决方案3】:

    我尝试了同样的事情,但我面临同样的问题。

    如果您使用的是通知内容扩展,您可以尝试其他解决方案。

    由于我们可以在ExtensionContaining App 中处理通知操作,您可以尝试在Extension 中使用extensionContext 而不是Containing App's UIApplication object 来处理它。 p>

    试试这个:

        func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void)
        {
            if response.actionIdentifier == "call"
            {
                self.extensionContext?.open(URL(string: "tel:9555221836")!, completionHandler: { (done) in
                    completion(.dismiss)
                })
            }
            //Handling other actions...
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-11
      • 2021-06-21
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2021-10-01
      相关资源
      最近更新 更多