我用兩個動作創建了一個通知。我的一個動作稱爲「取消」,而另一個稱爲「呼叫」。如何使「調用」操作運行一個URL,該URL位於我添加到代碼中的註釋中。這裏是我的代碼:如何創建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"])
}
}
也許下面將是有益的 http://useyourloaf.com/blog/openurl-deprecated-in-ios10/ https://stackoverflow.com/questions/38964264/openurl-in-ios10?rq=1 – CuriousRabbit