Skip to content

Instantly share code, notes, and snippets.

@yishai-glide
Created December 27, 2015 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yishai-glide/a88342a58b2d21cc816e to your computer and use it in GitHub Desktop.
Save yishai-glide/a88342a58b2d21cc816e to your computer and use it in GitHub Desktop.
public static void scheduleAutoCancel(@NonNull Context context) {
Intent intent = new Intent(context, MyReceiver.class);
AlarmManager manager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
int requestCode = REQUEST_CODE;
PendingIntent action = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long when = System.currentTimeMillis() + TIME_TO_WAUT_UNTIL_NOTIFICATION;
manager.set(AlarmManager.RTC, when, action);
}
public static class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentIntent(getNotificationIntent());
.setContentText("MESSAGE")
.setContentTitle("TITLE");
nm.notify(builder.build(), getNotificationId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment