Background Task Scheduler

Hello,

An application I am working on would like to schedule push notifications for a medication reminder app. I am trying to use BGTaskScheduler to wake up periodically and submit the notifications based on the user's medication schedule.

I set up the task registration in my AppDelegate's didFinishLaunchingWithOptions method:

        BGTaskScheduler.shared.register(
          forTaskWithIdentifier: backgroundTaskIdentifier,
          using: nil) { task in
            self.scheduleNotifications()
            task.setTaskCompleted(success: true)
            self.scheduleAppRefresh()
        }

        scheduleAppRefresh()

I then schedule the task using:

    func scheduleAppRefresh() {
        let request = BGAppRefreshTaskRequest(identifier: backgroundTaskIdentifier)
        request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 1)

        do {
            try BGTaskScheduler.shared.submit(request)
        } catch {
        }
    }

In my testing, I can see the background task getting called once, but if I do not launch the application during the day. The background task does not get called the next day.

Is there something else I need to add to get repeated calls from the BGTaskScheduler?

Thank You, JR

You don't schedule push notifications from the app. Do you mean you schedule local notifications?

In any case, it looks like you are expecting BGTaskScheduler to run every minute. That is not not going to happen, and this API is not meant to be used like that, to schedule actions that will run every minute or some such regular time interval.

BGTaskScheduler will run whenever it is appropriate for it to run. The earliestBeginDate only means to not run it before that time, not AT that time. You cannot specify when BGTaskScheduler will run.

I don't know what self.scheduleNotifications() does, but if you are trying to schedule notifications a minute at a time, that is not the correct use of this API.

What you need to do, assuming all the medication taking times are predetermined, to schedule all the notifications at once (you can schedule up to 64 active notifications), and whenever BGTaskScheduler runs or the app is launched by the user, you than then roll the notifications into the future as music as you are able to.

If this is to overcome that you cannot repeat the notification once it fires without scheduling separate notifications every minute, while your approach makes sense, it is not possible to implement it this way.

Speaking of push notifications, you can instead use push notifications and have your server send a notification every minute until the notification is acknowledged by the user.

Thank you for the feedback, and apologies for the confusion in my question.

Yes, I am referring to scheduling local notifications on the device. We have already completed the capability of receiving push notifications via FCM. We are trying to implement something similar using local notifications in case the user is offline.

I understand the earliestBeginDate field and the minute value was the last attempt I tried to get the task woken up after some time. I tried 30, 60, and 90 minutes, and the result was the same.

We are just attempting to wake at some interval, load the medication schedule, and make sure that all the notifications for that day/week have been scheduled.

Can you suggest a way to accomplish this in the offline state?

Regards, JR

Background Task Scheduler
 
 
Q