Search code examples
iosswiftnotificationslocalnotification

Change text of local notification in Swift


I developp an application for student. He can create his timetable. I tried to create a notification how summarize the next day. So I need to change the text notification, how can I do that?

Another example, the student can write his homework. One day before to return the homework, notify him.

I'have already tried to do a simple notification, but the text of the notification is static.

let content = UNMutableNotificationContent()                    
content.title = "Changement de semaine"                         
content.body = "Nous sommes en semaine \(notificationSemaineAB())"     
content.sound = UNNotificationSound.default                   

var dateComponent = DateComponents()
dateComponent.weekday = 2
dateComponent.hour = 11

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)

let request = UNNotificationRequest(identifier: "semaineAB", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)

For this one, I would like for each week, the text of notification tell me if we are in a even week or odd week. But in output, I have always the same text.


Solution

  • You can remove the old notification with this code

    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers:["semaineAB"])
    

    And after that, you can reuse your code to create a new one.