1

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

    }


}
}

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

4

3 回答 3

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

}
于 2017-02-09T21:31:26.067 回答
0

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

如果您使用的是Notification Content Extension ,您可以尝试其他解决方案。

由于我们可以在中处理通知操作ExtensionContaining App,因此您可以尝试在ExtensionusingextensionContext而不是 中处理它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...
    }
于 2017-02-21T10:43:15.077 回答
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()}
于 2017-02-10T01:56:35.057 回答