हम अधिसूचना एप्लिकेशन iOS की सामग्री को बदलते हैं

नमस्ते!

इस लेख में मैं आवेदन के अधिसूचना पाठ को बदलने के लिए स्पष्ट (कम से कम मेरे लिए) विकल्प का उपयोग करने के अपने अनुभव को साझा करना चाहता हूं, UNUserNotificationCenter के माध्यम से अतिरिक्त डेटा भेज रहा हूं।

मुझे उम्मीद है कि यह लेख स्विफ्ट पर आईओएस के लिए प्रोग्रामिंग में शुरुआती लोगों के लिए उपयोगी होगा। यह माना जाता है कि आपके पास iOS के लिए स्विफ्ट के साथ कम से कम कुछ अनुभव प्रोग्रामिंग है। मैंने स्विफ्ट 5 और एक्सकोड 10.2.1 का इस्तेमाल किया। और इसलिए, चलो शुरू करें।


कार्य


मेरे पास एक एप्लिकेशन है जो उपयोगकर्ता सूचनाओं को दिखाता है। एक अधिसूचना में बॉडी टेक्स्ट और बैकग्राउंड टेक्स्ट होते हैं।

इसलिए, यह विचार सबसे पहले उपयोगकर्ता को केवल मुख्य पाठ दिखाने के लिए है, और यदि उपयोगकर्ता द्वितीयक पाठ को जानना चाहता है, तो उसे अधिसूचना में "शो" बटन पर क्लिक करना होगा।

मैं एक अधिसूचना दिखाने के लिए निम्न विधि का उपयोग करता हूं (नीचे सभी स्पष्टीकरण):

1. सबसे पहले, हमें UNUserNotificationCenter की एक आवृत्ति को परिभाषित करना चाहिए:

class NotificationService: NSObject, UNUserNotificationCenterDelegate { let notificationCenter = UNUserNotificationCenter.current() } 


2. इसके बाद, हम एक विधि घोषित करते हैं जिसका उपयोग हम सूचनाएं भेजने के लिए करेंगे:
मेरी अधिसूचना अनुरोध विधि
 // 1 //   ,      Tuple    . // showBody -     Bool,           . // withAction -     Bool,       ,   ""  . // atDate date -    Date,      ,      . func showNotification(with item: (String, String), showBody: Bool, withAction: Bool, atDate date: Date) { // 2 //    let content = UNMutableNotificationContent() // 3 //     let userActionsIdentifier = “showMe” // 4 //      . content.title = item.0 // 5 //    ,      false,  ,       . if showBody { content.body = item.1 } // 6 //     -    notificationCenter userInfo      . content.userInfo = [item.0: item.1] content.sound = UNNotificationSound.default // 7 //        withAction  true,      "Show me". if withAction { content.categoryIdentifier = userActionsIdentifier } // 8 //    ,        . let notificationID = item.0 // 9 //      . //       ,      . var dc = DateComponents() dc.hour = Calendar.current.component(.hour, from: date) dc.minute = Calendar.current.component(.minute, from: date) dc.second = Calendar.current.component(.second, from: date) // 10 //   .     ,       UNCalendarNotificationTrigger. let trigger = UNCalendarNotificationTrigger(dateMatching: dc, repeats: false) // 11 //        ,   . let request = UNNotificationRequest(identifier: notificationID, content: content, trigger: trigger) // 12 //     notificationCenter,     . notificationCenter.add(request) { (error) in error == nil ? print(“notifacation request was added at “, trigger.nextTriggerDate()!) : print(error.debugDescription) } // 13 //    .            .      . let action = UNNotificationAction(identifier: “showMe”, title: “Show me”, options: []) // 14 //   . let category = UNNotificationCategory(identifier:userActionsIdentifier, actions: [action], intentIdentifiers: [], options: []) // 15 //      notificationCenter. notificationCenter.setNotificationCategories([category]) } 



जब हम इस पद्धति को कॉल करते हैं, तो हम उस समय एक अधिसूचना देखेंगे जो हम सेट करते हैं।
यदि उपयोगकर्ता अधिसूचना को नीचे खींचता है, तो "मुझे दिखाओ" बटन दिखाई देगा। तब उपयोगकर्ता को पृष्ठभूमि पाठ देखने के लिए इस बटन पर क्लिक करना होगा। ऐसा करने के लिए, हमें UNUserNotificationCenterDelegate प्रतिनिधि पद्धति का उपयोग करना होगा:

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


इसलिए, इस प्रतिनिधि विधि में मेरे द्वारा उपयोग किया जाने वाला कोड इस प्रकार है:

मेरी प्रतिनिधि विधि
 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(“didReceive response”) // 1 //   switch     switch response.actionIdentifier { case “showMe”: print(“showMe action”) // 2 //      userInfo,      showNotification   6,      String. let mainText = response.notification.request.content.userInfo.keys.first! as! String // 3 //       userInfo,      showNotification   6,       String. let subText = response.notification.request.content.userInfo.values.first! as! String // 4 //       showNotificationlet,    : // showBody    true,  withAction  false (       ). self.showNotification(with: (mainText, subText), showBody: true, withAction: false, atDate: Date(timeInterval: 3, since: Date())) default: print(“defaul action”) } completionHandler() } 



और यहाँ यह जैसा दिखता है:



परियोजना के अंदर (जिसे आप नीचे दिए गए लिंक से डाउनलोड कर सकते हैं) कुछ और कोड है, लेकिन विचार का सार इस लेख में वर्णित है।

धन्यवाद!

प्रोजेक्ट का लिंक

Source: https://habr.com/ru/post/hi451454/


All Articles