2016-12-13 7 views
5

मैंने दो कार्यों के साथ एक अधिसूचना बनाई। मेरे कार्यों में से एक को "रद्द करें" कहा जाता है, जबकि दूसरे को "कॉल" कहा जाता है। मैं "कॉल" एक्शन को एक यूआरएल कैसे चला सकता हूं जो एक टिप्पणी में है जिसे मैंने अपने कोड में जोड़ा है।एक यूआरएल कैसे चलाएं जब अधिसूचना कार्रवाई दबाई जाती है?

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 प्रोटोकॉल के अनुरूप होगा।

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) 
} 

विधि ऊपर अपनी सूचना को परिभाषित करने और यह समय निर्धारण के लिए जिम्मेदार होगा:

उदाहरण के लिए, अगर आप अपने AppDelegate में अपनी सूचना संभाल रहे हैं, तो आपको निम्न विधि की तरह कुछ हो सकता है। संक्षिप्तता के लिए, मैंने उन सभी कोड को छोड़ दिया है जो अधिसूचना को परिभाषित करेंगे क्योंकि आपने संकेत दिया है कि यह काम कर रहा है। इसके बजाय आपको ध्यान रखना चाहिए कि delegate संपत्ति self पर सेट की जा रही है।

फिर एक विस्तार में, आप AppDelegateUNUserNotificationCenterDelegate के अनुरूप बनाएंगे और आवश्यक विधियों को लागू करेंगे।

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) 
      } 
     } 
    } 
} 

अब क्योंकि आपके AppDelegateUNUserNotificationCenter के प्रतिनिधि, userNotification(_:didReceive:withCompletionHandler:) विधि के अपने कार्यान्वयन रूप UNUserNotificationCenterDelegate प्रोटोकॉल के अनुरूप है और आप self (AppDelegate) सेट बुलाया जाएगा।

+0

यह काम नहीं किया – krish

+0

@ कृष्ण क्या आपको कोई त्रुटि मिली? क्या आपने सिम्युलेटर या वास्तविक डिवाइस पर परीक्षण किया था? क्या अधिसूचना दिखाई दे रही है? –

+0

नहीं, मुझे कोई त्रुटि नहीं मिली, और मैंने इसे अपने वास्तविक फोन पर परीक्षण किया। अधिसूचना पॉप अप होती है, हालांकि जब मैं "कॉल" बटन पर टैप करता हूं, तो यह मुझे किसी को कॉल करने के लिए यूआरएल चलाने के बजाय ऐप में लाता है। – krish

संबंधित मुद्दे