I am trying to follow this post and this post. However, in my implemented code, the notification fires within 3 seconds even though I set the delay to 60000ms. I am wondering what I have wrong. Here is my code
private void displayNotifiation(Contact contact, byte[] img) {
int notificationId = contact.hashCode();
Log.d(TAG, "onClick 2: put info="+contact.getName()+" notifId:"+notificationId);
Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, MainActivity.CHANNEL_ID)
.setSmallIcon(R.mipmap.logo2)
.setContentTitle("It's time to reach out to "+contact.getName())
.setContentText("For your health")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setLargeIcon(bitmap)
.setAutoCancel(false);
Intent intent = new Intent(mContext, ActivityNotificationLanding.class);
Uri uri = Uri.parse("http://notexist.mykeepintouch.app/contactid/"+contact.getId());
intent.setData(uri);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
gson.putExtra(intent, IntentFlashStore.CONTACT_KEY, contact);
NotificationInfo info = new NotificationInfo();
info.setId(notificationId);
gson.putExtra(intent, IntentFlashStore.NOTIFICATION_INFO, info);
PendingIntent activity = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(activity);
Notification notification = builder.build();
Intent notificationIntent = new Intent(mContext, MyNotificationPublisher.class);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + 60000;
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
}
Hmmmm,
SIDE QUESTION: Will this work across a reboot of the phone though? I read something about a BootReceiver and not sure if I need to do that. Android 24+