Created
October 7, 2017 08:55
-
-
Save Tarelochkin/6ec6d9c511463b03ce63b071e32c8b9c to your computer and use it in GitHub Desktop.
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
private static final int WATER_REMINDER_NOTIFICATION_ID = 1138; | |
/** | |
* This pending intent id is used to uniquely reference the pending intent | |
*/ | |
private static final int WATER_REMINDER_PENDING_INTENT_ID = 3417; | |
private static final int ACTION_DRINK_PENDING_INTENT_ID = 1; | |
private static final int ACTION_IGNORE_PENDING_INTENT_ID = 14; | |
public static void remindUserBecauseCharging(Context context) { | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) | |
.setColor(ContextCompat.getColor(context, R.color.colorPrimary)) | |
.setSmallIcon(R.drawable.ic_drink_notification) | |
.setLargeIcon(largeIcon(context)) | |
.setContentTitle(context.getString(R.string.charging_reminder_notification_title)) | |
.setContentText(context.getString(R.string.charging_reminder_notification_body)) | |
.setStyle(new NotificationCompat.BigTextStyle().bigText( | |
context.getString(R.string.charging_reminder_notification_body))) | |
.setDefaults(Notification.DEFAULT_VIBRATE) | |
.setContentIntent(contentIntent(context)) | |
.addAction(drinkWaterAction(context)) | |
.addAction(ignoreReminderAction(context)) | |
.setAutoCancel(true); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
notificationBuilder.setPriority(Notification.PRIORITY_HIGH); | |
} | |
NotificationManager notificationManager = (NotificationManager) | |
context.getSystemService(Context.NOTIFICATION_SERVICE); | |
/* WATER_REMINDER_NOTIFICATION_ID allows you to update or cancel the notification later on */ | |
notificationManager.notify(WATER_REMINDER_NOTIFICATION_ID, notificationBuilder.build()); | |
} | |
private static Action ignoreReminderAction(Context context) { | |
Intent ignoreReminderIntent = new Intent(context, WaterReminderIntentService.class); | |
ignoreReminderIntent.setAction(ReminderTasks.ACTION_DISMISS_NOTIFICATION); | |
PendingIntent ignoreReminderPendingIntent = PendingIntent.getService( | |
context, | |
ACTION_IGNORE_PENDING_INTENT_ID, | |
ignoreReminderIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
Action ignoreReminderAction = new Action(R.drawable.ic_cancel_black_24px, | |
"No, thanks.", | |
ignoreReminderPendingIntent); | |
return ignoreReminderAction; | |
} | |
private static Action drinkWaterAction(Context context) { | |
Intent incrementWaterCountIntent = new Intent(context, WaterReminderIntentService.class); | |
incrementWaterCountIntent.setAction(ReminderTasks.ACTION_INCREMENT_WATER_COUNT); | |
PendingIntent incrementWaterPendingIntent = PendingIntent.getService( | |
context, | |
ACTION_DRINK_PENDING_INTENT_ID, | |
incrementWaterCountIntent, | |
PendingIntent.FLAG_CANCEL_CURRENT); | |
Action drinkWaterAction = new Action(R.drawable.ic_local_drink_black_24px, | |
"I did it!", | |
incrementWaterPendingIntent); | |
return drinkWaterAction; | |
} | |
private static PendingIntent contentIntent(Context context) { | |
Intent startActivityIntent = new Intent(context, MainActivity.class); | |
return PendingIntent.getActivity( | |
context, | |
WATER_REMINDER_PENDING_INTENT_ID, | |
startActivityIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
} | |
private static Bitmap largeIcon(Context context) { | |
Resources res = context.getResources(); | |
Bitmap largeIcon = BitmapFactory.decodeResource(res, R.drawable.ic_local_drink_black_24px); | |
return largeIcon; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment