This file contains hidden or 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
//we create a class that extends Worker | |
public class UpdateEventsWorker extends Worker { | |
//here we define the constants for the remote URL LiveData tag and | |
//the filename of the file we'll be exporting the JSON to | |
private static final String REMOTE_URL = "REMOTE_URL"; | |
private static final String JSON_RESULT = "JSON_STRING"; | |
public UpdateEventsWorker(@NonNull Context context, @NonNull WorkerParameters params) { | |
super(context, params); | |
} |
This file contains hidden or 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
//firstly, we create a new piece of LiveData to pass the remote URL to the | |
// Worker, and for that we define the REMOTE_URL String and initialize it | |
// by calling the getRemoteURL() method in our app | |
Data remoteUrl = new Data.Builder().putString(REMOTE_URL, getRemoteURL()).build(); | |
//then, we create a new OneTimeWorkRequest since we don't need it to be | |
//periodic, and set InputData to what we defined above, and add a work | |
//tag just in case we want to cancel this work | |
OneTimeWorkRequest getEventJson = new OneTimeWorkRequest | |
.Builder(UpdateEventsWorker.class) |
This file contains hidden or 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
WorkManager.getInstance(context).enqueue(notificationWork); | |
//alternatively, we can use this form to determine what happens to the existing stack | |
// WorkManager.getInstance(context).beginUniqueWork(workTag, ExistingWorkPolicy.REPLACE, notificationWork); | |
//ExistingWorkPolicy.REPLACE - Cancel the existing sequence and replace it with the new one | |
//ExistingWorkPolicy.KEEP - Keep the existing sequence and ignore your new request | |
//ExistingWorkPolicy.APPEND - Append your new sequence to the existing one, | |
//running the new sequence's first task after the existing sequence's last task finishes |
This file contains hidden or 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
//we set a tag to be able to cancel all work of this type if needed | |
public static final String workTag = "notificationWork"; | |
//store DBEventID to pass it to the PendingIntent and open the appropriate event page on notification click | |
Data inputData = new Data.Builder().putInt(DBEventIDTag, DBEventID).build(); | |
// we then retrieve it inside the NotifyWorker with: | |
// final int DBEventID = getInputData().getInt(DBEventIDTag, ERROR_VALUE); | |
OneTimeWorkRequest notificationWork = new OneTimeWorkRequest.Builder(NotifyWorker.class) | |
.setInitialDelay(calculateDelay(event.getDate()), TimeUnit.MILLISECONDS) |
This file contains hidden or 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
public class NotifyWorker extends Worker { | |
public NotifyWorker(@NonNull Context context, @NonNull WorkerParameters params) { | |
super(context, params); | |
} | |
@NonNull | |
@Override | |
public Result doWork() { | |
// Method to trigger an instant notification | |
triggerNotification(); |
This file contains hidden or 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
private final static String manasia_notification_channel = "Manasia Event Reminder"; | |
//get latest event details | |
String notificationTitle = "Manasia event: " + event.getTitle(); | |
String notificationText = event.getDate() + " at Stelea Spatarul 13, Bucuresti"; | |
//build the notification | |
NotificationCompat.Builder notificationBuilder = | |
new NotificationCompat.Builder(getApplicationContext(), manasia_notification_channel) | |
.setSmallIcon(R.drawable.ic_manasia_small) |
This file contains hidden or 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
//create an intent to open the event details activity | |
Intent intent = new Intent(getApplicationContext(), EventDetail.class); | |
//put together the PendingIntent | |
PendingIntent pendingIntent = | |
PendingIntent.getActivity(getApplicationContext(), 1, intent, FLAG_UPDATE_CURRENT); |
This file contains hidden or 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
private final static String manasia_notification_channel = "Manasia Event Reminder"; | |
// Create the NotificationChannel, but only on API 26+ because | |
// the NotificationChannel class is new and not in the support library | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
//define the importance level of the notification | |
int importance = NotificationManager.IMPORTANCE_DEFAULT; | |
//build the actual notification channel, giving it a unique ID and name | |
NotificationChannel channel = |
NewerOlder