【问题标题】:How to make a URL Run when a notification action is pressed?按下通知操作时如何使 URL 运行?
【发布时间】:2017-04-29 00:33:41
【问题描述】:

我创建了一个包含两个操作的通知。我的一项操作称为“取消”,而另一项操作称为“呼叫”。如何使“呼叫”操作运行我添加到代码中的注释中的 URL。这是我的代码:

  func notificationFires(){

    /**
     URL Code:

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


**/

    let call = UNNotificationAction(identifier:"call", title:"Call", options:[.foreground])
    let cancel = UNNotificationAction(identifier: "cancel", title: "Cancel", options: [.destructive ])
    let category = UNNotificationCategory(identifier: "category", actions: [call, cancel], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])

    let notification = UILocalNotification()
    notification.category = "category"
    // 2

    notification.soundName = UILocalNotificationDefaultSoundName
    notification.fireDate = datePicker.date

    // 3
    if textField.text == "" {

        notification.alertBody = "You have a call right now!"

    }
    else{

        notification.alertBody = self.textField.text

    }
    // 4
    notification.timeZone = NSTimeZone.default
    // 5
    // 6
    notification.applicationIconBadgeNumber = 1
    // 7

    func application(application: UIApplication!, handleActionWithIdentifier identifier:String!, forLocalNotification notification:UILocalNotification!, completionHandler: (() -> Void)!){

        if (identifier == "call"){
            if let url = URL(string: "tel://2162964785") {
                UIApplication.shared.open(url, options: [:])
            }
        }else if (identifier == "cancel"){

        }

    }

    UIApplication.shared.scheduleLocalNotification(notification)



    func application(application: UIApplication,  didReceiveLocalNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        print("Recieved: notification")



        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: ["notification"])




    }

}

【问题讨论】:

标签: swift xcode swift2 swift3


【解决方案1】:

假设您的通知工作正常,您可以按照UNUserNotificationCenterDelegate 处理“呼叫”操作。

类似:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.actionIdentifier == "call" {
            let body = response.notification.request.content.body

            if let url = URL(string: "tel://\(body)") {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
    }

body 常量将设置为您要“打开”的电话号码。

另外,重要的是要注意这必须在实际设备上进行测试。打开tel 方案在模拟器中没有任何作用。

UNUserNotificationCenterDelegate API 参考: https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate

编辑:

不要调用委托方法。相反,你实现它。委托方法由UNUserNotificationCenter 调用。

要使其正常工作,确保将 UNUserNotificationCenter.current() 委托属性设置为符合 UNUserNotificationCenterDelegate 协议的类非常重要。

例如,如果您正在处理AppDelegate 中的通知,您可能有类似以下方法:

func callNotification() {
    let center = UNUserNotificationCenter.center()

    // TODO: - Create your actions, category, content, trigger and request...

    center.delegate = self // Important!

    center.removeAllPendingNotificationRequests()
    center.add(request, withCompletionHandler: nil)
}

上述方法将负责定义您的通知并安排它。为简洁起见,我省略了定义通知的所有代码,因为您表示这是有效的。相反,您应该注意 delegate 属性被设置为 self

然后在扩展中,您将使AppDelegate 符合UNUserNotificationCenterDelegate 并实现所需的方法。

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.actionIdentifier == "call" {
            let body = response.notification.request.content.body

            if let url = URL(string: "tel://\(body)") {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
    }
}

现在因为您的AppDelegate 符合UNUserNotificationCenterDelegate 协议,并且您将self (AppDelegate) 设置为UNUserNotificationCenter 的代表,您的实现 userNotification(_:didReceive:withCompletionHandler:)方法将被调用。

【讨论】:

  • @Krish 你收到错误了吗?你是在模拟器还是真机上测试?通知出现了吗?
  • 不,我没有收到错误,我确实在我的实际手机上进行了测试。通知确实会弹出,尽管当我点击“呼叫”按钮时,它所做的只是将我带入应用程序,而不是运行 URL 来呼叫某人。
  • @Krish 它应该将您带到您的应用程序,userNotificationCenter(_:didReceive:withCompletionHandler:) 方法应该处理 tel 方案的打开。委托方法是否被调用?
  • 我需要在哪里调用委托方法?
  • 我该如何调用委托方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多