Search code examples
iosswiftuilocalnotificationlocalnotification

Running local notifications with hourly interval every day for specific amount of time


How can I make my app to be able to fire local notifications every day with customisable interval but only for allocated amount of time, for example: from 10:00 to 20:00pm?

Now I only implemented repeated notifications with custom interval:

    func beginNotifications(_ config: UserConfig) {

    let interval = config.notificationInterval

    let center =  UNUserNotificationCenter.current()
    center.removeAllPendingNotificationRequests()

    let content = UNMutableNotificationContent()
    content.title = "Content title"
    content.subtitle = "Content body"
    content.sound = UNNotificationSound.default

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)

    let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)

    center.add(request)

}

By far I only came up with solution - to make two separate methods using Timer, which will start and stop notification function daily, and enable Background Mode for the app.


Solution

  • The first solution came to my mind if scheduling the local push notification N-times (how many times as you want). You just need to make the identifier unique of course.

    enter image description here


    By far I only came up with solution - to make two separate methods using Timer, which will start and stop notification function daily, and enable Background Mode for the app.

    This isn't recommended imo, and if you push this, you might need to deal with Apple review team.