0

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+

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • 2
    `RTC` alarms are based on "wall clock" time. `SystemClock.elapsedRealtime()` is time since boot, so your alarm is set in the past, and it fires as soon as it can. For `RTC`, you can base your time off of `System.currentTimeMillis()`. And no, it does not work across reboots. You'll need to reset your alarm in a boot Receiver. – Mike M. Aug 02 '20 at 00:31
  • @MikeM. How do I mark you comment as the right answer ;). thanks! – Dean Hiller Aug 02 '20 at 19:44
  • You can't, but it's cool. :-) This is a pretty common issue, so I would mark this as a duplicate, rather than repeat an answer again. I wanted to wait until you'd confirmed that that was the only problem, though. If you'd like, we'll call it a freebie, and you can just delete this. Otherwise, let me know, and I'll find an appropriate duplicate later on, when I get a chance. Thank you, though. I do appreciate the offer. Glad it helped. Cheers! – Mike M. Aug 02 '20 at 20:07
  • I keep my questions as an index for my search dictionary. yes, you can close as duplicate. it still shows up in my index(I tend to use same search terms and run into my own questions so prefer to keep them indexed in google). – Dean Hiller Aug 02 '20 at 20:39

0 Answers0