Reputation: 33
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
Reputation: 425
I would use a separate class to handle all notification functions
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
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
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