Skip to content

Instantly share code, notes, and snippets.

@Ayush783
Created February 10, 2024 18:06
Show Gist options
  • Save Ayush783/525c83529e049c133ca7be3f1d52a9e2 to your computer and use it in GitHub Desktop.
Save Ayush783/525c83529e049c133ca7be3f1d52a9e2 to your computer and use it in GitHub Desktop.
Register push notification worker requests
List < OneTimeWorkRequest > notificationScheduleList = new ArrayList();
// create list of OneTimeRequests for re triggering notifications every 5 minutes after
// a new notification is received
for (int i = 1; i < 3; i++) {
notificationScheduleList.add(
new OneTimeWorkRequest.Builder(PushNotificationSchedulerWork.class) // Specify the worker
.setInitialDelay(30 * i, TimeUnit.MINUTES) // First one at 30th minute, Next at 60th minute
.addTag("PushNotificationSchedulerWork") // Specify a work tag
.build()
);
}
// enqueue the notification schedule list as a unique work
// This will ensure that multiple work are not enqueued for the
// same job because of ExistingWorkPolicy.REPLACE
WorkManager.getInstance(context).enqueueUniqueWork(
"PushNotificationSchedulerWork",
ExistingWorkPolicy.REPLACE,
notificationScheduleList
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment