gpt4 book ai didi

swift - 如何在按下通知操作时运行 URL?

转载 作者:搜寻专家 更新时间:2023-10-31 08:06:08 24 4
gpt4 key购买 nike

我创建了一个包含两个操作的通知。我的一个 Action 叫做“取消”,而另一个叫做“调用”。如何让“调用”操作运行我添加到代码中的注释中的 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"])




}

}

最佳答案

假设您的通知工作正常,您可以符合 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 scheme 在模拟器中什么都不做。

UNUserNotificationCenterDelegate API 引用: https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate

编辑:

不调用委托(delegate)方法。相反,你实现它。 UNUserNotificationCenter 调用委托(delegate)方法.

要让它正常工作,重要的是要确保您设置了 UNUserNotificationCenter.current()将属性委托(delegate)给符合 UNUserNotificationCenterDelegate 的类协议(protocol)。

例如,如果您在 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协议(protocol),你设置 self ( AppDelegate ) 作为 UNUserNotificationCenter的委托(delegate),您的实现 userNotification(_:didReceive:withCompletionHandler:)方法将被调用。

关于swift - 如何在按下通知操作时运行 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41129728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com