2016-12-13 60 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