0

I have an app where the user needs to complete a task every day. If the user does not complete a task, he/she gets a reminder at 8am the next day with a phrase prompting to complete the task.

We would like to send a phrase every morning but we don't want it to be the same phrase every day.

This is what we have right now:

static func scheduleDailyUnwatchedNotification() {
    
    let notificationMessages = ["Phrase one", "Phrase two", "Phrase 3", "Phrase 4", "Phrase 5"]
    let totalMessages = notificationMessages.count
    let randomIndex = Int.random(in: 0..<totalMessages)
    
    let center = UNUserNotificationCenter.current()
    center.removePendingNotificationRequests(withIdentifiers: ["dailyReminder"])
    
    let content = UNMutableNotificationContent()
    content.title = "Reminder"
    content.body = notificationMessages[randomIndex]
    content.sound = .default
    
    var dateComponents = DateComponents()
    dateComponents.hour = 8
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    
    let request = UNNotificationRequest(identifier: "dailyReminder", content: content, trigger: trigger)
    
    center.add(request)
}

The problem is that even though a random phrase is selected, the notification will always repeat with that same random phrase.

How can we have it repeat with a different phrase?

cesarcarlos
  • 1,271
  • 1
  • 13
  • 33
  • You need to schedule individual notifications for each day, not a single repeating notification – Paulw11 Jul 14 '21 at 22:05

1 Answers1

0

You’ll need to schedule each different phrased notification manually. However if you’re sending a phrase every day and you have say 50 of them you could schedule each to repeat every 50 days. Then whenever the user opens the app you can always swap around the days the notifications are sent - so the phrases ordering isn’t always the same. It’s not the most ideal but does allow recurring notifications with different titles.

Alternatively if you want to be able to change the notification titles without new app releases/have more control you can use push notifications. This way you can set up a backend to send messages but it does have more overhead from a server perspective.

Justin
  • 1,318
  • 9
  • 12