2016-12-13 62 views
5

我用两个动作创建了一个通知。我的一个动作称为“取消”,而另一个称为“呼叫”。如何使“调用”操作运行一个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"]) 




    } 

} 
+0

也许下面将是有益的 http://useyourloaf.com/blog/openurl-deprecated-in-ios10/ https://stackoverflow.com/questions/38964264/openurl-in-ios10?rq=1 – CuriousRabbit

回答

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协议并设置selfAppDelegate)作为UNUserNotificationCenter的委托,您的userNotification(_:didReceive:withCompletionHandler:)方法的实现将被调用。

+0

这没有奏效 – krish

+0

@Krish你有错误吗?你在模拟器或实际设备上测试过吗?通知是否出现? –

+0

不,我没有得到一个错误,我在我的实际电话上测试过它。虽然当我点击“呼叫”按钮时,通知会弹出,但它所做的只是让我进入应用程序,而不是运行URL来呼叫某人。 – krish