📜 ⬆️ ⬇️

Change the contents of the iOS app notification

Hello!

In this article, I want to share my experience of using one not obvious (at least for me) option to change the application notification text, sending additional data through the UNUserNotificationCenter.

I hope this article will be useful for beginners in iOS programming on Swift. It is assumed that you have at least some programming experience under iOS on Swift. I used Swift 5 and Xcode 10.2.1. And so, let's get started.
')

Task


I have an application that shows the user a notification. The notification consists of a main text and a secondary text.

So the idea is to first show the user only the main text, and if the user wants to know the secondary text, he should click the “show” button in the notification.

I use the following method to display a notification (all explained below):

1. First of all, we need to define an instance of UNUserNotificationCenter:

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


2. Next, we declare a method that we will use to send notifications:
My notification request method
 // 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]) } 



When we call this method, we will see a notification at a certain time.
If the user pulls down the notification, the button “Show me" will appear. Then the user must click this button to see the background text. To do this, we must use the delegate method UNUserNotificationCenterDelegate:

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


So, the code that I use in the method of this delegate is as follows:

My delegate method
 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() } 



And this is what it looks like:



Inside the project (which you can download from the link below) there is some more code, but the very essence of the idea is described in this article.

Thank!

Link to the project

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


All Articles