点击我的通知操作后,我的网址未运行。我该怎么办?

时间:2017-02-09 21:19:27

标签: ios swift url unusernotificationcenter usernotifications

在我的应用中,我希望在点击通知操作时运行URL。问题是,当我运行 app 时,它会将我置于前台,但不会运行URL(我将.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: [:])                            
            }                

    }


}
}

Anyones帮助会很棒:)感谢您的回答!

3 个答案:

答案 0 :(得分:0)

对于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)")

}

答案 1 :(得分:0)

试试这个:

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()}

答案 2 :(得分:0)

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

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

由于我们可以处理Extension以及Containing App 中的通知操作,您可以尝试使用ExtensionextensionContext中处理通知操作而不是Containing App's UIApplication object

试试这个:

    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...
    }