Created
February 10, 2024 18:06
-
-
Save Ayush783/525c83529e049c133ca7be3f1d52a9e2 to your computer and use it in GitHub Desktop.
Register push notification worker requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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