Local Notification (3 times a day) with different body text

Hey,

my aim is to build a local notification which should be triggered at three specific times a day. The notification body should be a different one every day.

This is the String Array of which one string should be shown in the notifiaction.

It doesn´t matter if the String gets choosen randomly or in the sequence the array is ordered.


var arrayText: [String] = ["1",
                           "2",
                           "3",
                           "4",
                           "5"]


This is how far I got. Just a normal local notification with everytime the same body text and only one trigger a day, but I want 3 triggers a day and not everytime the same body text.


              let center = UNUserNotificationCenter.current()
              
              center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
                  if !granted {
                      print("Something went wrong")
                  }
              }
              
              let content = UNMutableNotificationContent()
              content.title = "text"
              content.body = "text"
              content.sound = UNNotificationSound.default
              
              let gregorian = Calendar(identifier: .gregorian)
              let now = Date()
              var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now

              components.hour = 8
              components.minute = 0
              components.second = 0
              
              
              let date = gregorian.date(from: components)!
              
              
              let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: date)
               
              let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
              
              let uuidString = UUID().uuidString
              let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)

              center.add(request, withCompletionHandler: { (error) in
                  if let error = error {
                      // Something went wrong
                  }
              })
             


Help would be awesome. And thank you for every helpfull answer

Yours sincerely

Tell aka. Relbot

Accepted Reply

Thank you for your anser,

Sorry but you are wrong the notification triggers at 8am I tested it. I know that I set only one specific time. That is how far I got.

Maybe to make it easier to explain.

I would like to create one notification, which should consist randomly out of many different body texts the user could get. Is there any possible way to do this.

There is a way if I understand that right.


Isn´t the content.userInfo the info my app gets when the user presses on my notifiaction. If I understood that right.


I am not a profesional developer. Would be awesome if you can help me a little bit with the code.

regards Tell

Replies

I do not see where you set the variable date.


Line 23, date is midnight and never changes.

So line 31, you trigger at midnight.


So create 3 notification and set their date as needed, but not 0:00:00


The same for the content.

Where do you define variable content ?


You should pass it in content.userInfo dictionary


Read here how it works

h ttps://www.hackingwithswift.com/example-code/system/how-to-set-local-alerts-using-unnotificationcenter

Thank you for your anser,

Sorry but you are wrong the notification triggers at 8am I tested it. I know that I set only one specific time. That is how far I got.

Maybe to make it easier to explain.

I would like to create one notification, which should consist randomly out of many different body texts the user could get. Is there any possible way to do this.

There is a way if I understand that right.


Isn´t the content.userInfo the info my app gets when the user presses on my notifiaction. If I understood that right.


I am not a profesional developer. Would be awesome if you can help me a little bit with the code.

regards Tell