Loïc Mazuc
Loïc Mazuc

Reputation: 33

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.

Upvotes: 1

Views: 2126

Answers (3)

theNoobDev10
theNoobDev10

Reputation: 425

I would use a separate class to handle all notification functions

  • First Create a function which takes a string in your Notification Class
  • Then use a bool value to check between even and odd week in your ViewController
  • And also a function to put the logic for even/odd week

In Notifications.swift

class Notifications: NSObjcet, UNUserNotificationCenterDelegate {

func weekTrigger(bodyText: String) {

let content = UNMutableNotificationContent() 

content.body = bodyText

let request = .......// 
}

Now in your ViewController

class ViewController: UIViewController {

var aBoolToCheckBtwnEvenOrOddWeek: Bool  //you can set an initial value

let notificationManager = Notifications()

}

Somewhere in VC where you see fit add the logic to check between even and odd week

func someFunction() {

if aBoolToCheckBtwnEvenOrOddWeek == true {   // which means even in my case

notificationManager.weekTrigger(bodyText: evenWeekString) // call the function 

} else {

notificationManager.weekTrigger(bodyText: oddWeekString)

}
}

Now add this function wherever you want in your ViewController!

Happy Coding :]

Upvotes: 0

Tomas P.
Tomas P.

Reputation: 21

You can create a custom class that extends UNMutableNotificationContent. That will determine the content of the notification based on the even/odd week. Then you can use another class to instantiate the notification request usign a method of the class of type UNMutableNotificationContent.

Upvotes: 0

m1sh0
m1sh0

Reputation: 2351

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.

Upvotes: 1

Related Questions