2016-09-08 8 views
8

तो मैं इस तरह की सूचनाएं शेड्यूल करने में सक्षम हूं;आईओएस में उपयोगकर्ता नोटिफिकेशन क्रियाओं को कैसे संभालें 10

//iOS 10 Notification 
if #available(iOS 10.0, *) { 

    var displayDate: String { 
     let dateFormatter = DateFormatter() 
     dateFormatter.dateStyle = DateFormatter.Style.full 
     return dateFormatter.string(from: datePicker.date as Date) 
    } 
    let notif = UNMutableNotificationContent() 


    notif.title = "I am a Reminder" 
    notif.subtitle = "\(displayDate)" 
    notif.body = "Here's the body of the notification" 
    notif.sound = UNNotificationSound.default() 
    notif.categoryIdentifier = "reminderNotification" 

    let today = NSDate() 
    let interval = datePicker.date.timeIntervalSince(today as Date) 

    let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false) 

    let request = UNNotificationRequest(identifier: "reminderNotif", content: notif, trigger: notifTrigger) 

    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in 
     if error != nil { 
      print(error) 
      // completion(Success: false) 
     } else { 
      //completion(Sucess: true) 
     } 
     }) 
} 

मैं appDelegate में अनुमतियों के लिए कहा है और सूचनाएं अधिसूचना एक्सटेंशन का उपयोग कर अपने कस्टम दृश्य के साथ ठीक दिखाई देते हैं।

मैंने अधिसूचना श्रेणी के लिए appDelegate में अधिसूचना क्रियाएं जोड़ दी हैं; ये भी दिखाई देते हैं।

//Notifications Actions 

private func configureUserNotifications() { 
    if #available(iOS 10.0, *) { 

     let tomorrowAction = UNNotificationAction(identifier: "tomorrowReminder", title: "Remind Me Tomorrow", options: []) 

     let dismissAction = UNNotificationAction(identifier: "dismissReminder", title: "Dismiss", options: []) 


     let category = UNNotificationCategory(identifier: "reminderNotification", actions: [tomorrowAction, dismissAction], intentIdentifiers: [], options: [.customDismissAction]) 

     UNUserNotificationCenter.current().setNotificationCategories([category]) 

    } else { 
     // Fallback on earlier versions 
    } 
} 

मैं अधिसूचना विस्तार .plist फ़ाइल में सेट एक ही श्रेणी की है। और अधिसूचना विस्तार में जब उपयोगकर्ता कार्रवाई पर टैप करता है तो टेक्स्ट को बदलने के लिए मेरे पास निम्न है।

//Handle Notification Actions And Update Notification Window 


private func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) { 

    if response.actionIdentifier == "tomorrowReminder" { 
     print("Tomrrow Button Pressed") 
     subLabel.text = "Reminder For Tomorrow" 
     subLabel.textColor = UIColor.blue 
     done(.dismissAndForwardAction) 
    } 

    if response.actionIdentifier == "dismissReminder" { 
     print("Dismiss Button Pressed") 
     done(.dismiss) 

    } else { 
     print("Else response") 
     done(.dismissAndForwardAction) 
    } 

} 

हालांकि टेक्स्ट नहीं बदलता है और कोई भी कथन नहीं कहा जाता है;

ऐप में से अधिक मेरे पास निम्नलिखित है;

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 
    if #available(iOS 10.0, *) { 
     UNUserNotificationCenter.current().delegate = self 
     configureUserNotifications() 

    } 
} 

extension AppDelegate: UNUserNotificationCenterDelegate { 

@available(iOS 10.0, *) 
private func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
    completionHandler([.alert, .sound]) 
} 

@available(iOS 10.0, *) 
private func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 

    print("Recieved Action For \(response.actionIdentifier)") 

    if response.actionIdentifier == "tomorrowReminder" { 
     print("Tomorrow Reminder") 


     //Set new reminder for tomorrow using the notification content title 



     completionHandler() 
    } 

    if response.actionIdentifier == "dismissReminder" { 
     print("Dismiss Reminder...") 
     completionHandler() 
    } 
} 

} 

न तो इन कार्यों में से वास्तव में या तो appDelegate में कहा जाता है। मुझे यकीन नहीं है कि एक्सटेंशन दृश्य को अपडेट करने में समस्या ऐप प्रतिनिधि से संबंधित है या नहीं। मुझे ऐसा नहीं लगता, मैंने ऐप्पल के डब्ल्यूडब्ल्यूडीसी वीडियो के साथ-साथ अन्य ट्यूटोरियल का पालन किया है और दस्तावेज़ एपीआई को देखा है और पता नहीं लगा सकता;

  • अधिसूचना विस्तार टेक्स्ट लेबल क्यों अपडेट नहीं कर रहे हैं?
  • ऐप डिलीगेट में फ़ंक्शंस क्यों नहीं बुलाए जा रहे हैं?
  • कार्रवाई के लिए का उपयोग करने के लिए ऐप प्रतिनिधि में अधिसूचना सामग्री का उपयोग कैसे कर सकता हूं?

पीएस: मैंने पिछले कुछ हफ्तों में शोध किया है और इसे समझने की कोशिश की है, यह काफी सीधे आगे लग रहा था और मुझे यकीन है कि मैं क्या खो रहा हूं। मुझे पता है कि मैं इन मुद्दों के साथ अकेला नहीं हूं।

उत्तर

4

मैं तुम्हारा के पूरे कोड की जाँच नहीं की है, लेकिन कम से कम, इन समारोह हैडर परिवर्तित करने की आवश्यकता:

func userNotificationCenter(_ center: UNUserNotificationCenter, 
          willPresent notification: UNNotification, 
          withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

func userNotificationCenter(_ center: UNUserNotificationCenter, 
          didReceive response: UNNotificationResponse, 
          withCompletionHandler completionHandler: @escaping() -> Void) { 

func didReceive(_ response: UNNotificationResponse, 
       completionHandler done: @escaping (UNNotificationContentExtensionResponseOption) -> Void) { 

सरल नियम: private निकालें, @escaping जोड़ें।

आपको एक्सकोड से गलत सुझाव मिल सकते हैं, लेकिन उन्हें private बनाने के साथ, उद्देश्य-सी प्रविष्टि बिंदु उत्पन्न नहीं होते हैं। आईओएस रनटाइम आंतरिक रूप से उद्देश्य-सी चयनकर्ताओं का उपयोग करता है, इसलिए यह आपके तरीकों को नहीं ढूंढ सकता है, इस प्रकार, उन्हें निष्पादित नहीं किया जाता है।

+0

'एस्केपिंग' का अर्थ क्या है और क्या करता है? – Honey

+0

ओम धन्यवाद! क्या आप जानते हैं कि मैं एक नई अधिसूचना शेड्यूल करने के लिए ऐप प्रतिनिधि में 'notification.request.content.title' कैसे कर सकता हूं? उदाहरण के लिए, मैं एक अनुस्मारक शेड्यूल करने की कोशिश कर रहा हूं और कल –

+0

के लिए फिर से याद दिलाने के लिए एक कार्रवाई कर रहा हूं, इससे कोई फर्क नहीं पड़ता कि मुझे 'प्रतिक्रिया.notification.request.content.title' –

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