Skip to content

Instantly share code, notes, and snippets.

View adriantache's full-sized avatar

Adrian Tache adriantache

View GitHub Profile
@adriantache
adriantache / NotificationChannel.java
Last active May 11, 2018 06:35
Notification Channel code
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 =
@adriantache
adriantache / NotificationPendingIntent.java
Last active May 11, 2018 06:19
Notification PendingIntent code
//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);
@adriantache
adriantache / Notification.java
Last active May 22, 2018 15:41
Notification Building code
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)
@adriantache
adriantache / WorkerCustomClass.java
Last active May 7, 2019 16:00
Worker Custom Class
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();
//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)
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
//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)
//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);
}
//WARNING: this code is no longer valid as of WorkManager-alpha-10, please
//refer to this post for more details: https://medium.com/p/8e49c463cbf7
//finally, we get the status from the WorkManager by id since we still have
//a reference, otherwise we could do it by tag, and create an observer which
//checks whether the Worker has succeeded (i.e. returned Worker.Result.SUCCESS)
WorkManager.getInstance()
.getStatusById(getEventJson.getId())
.observe(MainActivity.this, workStatus -> {
if (workStatus != null && workStatus.getState().equals(State.SUCCEEDED)) {
//build an immediate OneTimeWorkRequest to fetch events from remote
OneTimeWorkRequest getEventJson = new OneTimeWorkRequest
.Builder(UpdateEventsWorker.class)
.setInputData(remoteUrl)
.addTag(EVENTS_JSON_WORK_TAG)
.build();
//we get a ListenableFuture<Void> from enqueue() which we can use below
//using beginUniqueWork to prevent re-enqueuing the same task while it is already running
ListenableFuture<Void> listenableFuture = WorkManager.getInstance().beginUniqueWork(EVENTS_JSON_WORK_TAG,