2

I want to show notification at defined time. I use "setWhen()". But any argument of setWhen() is ignoring and notification always showing instantly. What I did wrong? My code:

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
                    .setWhen(???)
                    .setSmallIcon(R.drawable.app_icon)
                    .setContentTitle("Test")
                    .setContentText("Test");
    Intent resultIntent = new Intent(this, MainActivity.class);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
92sandeor
  • 31
  • 6
  • take a look to my question and the answer. I fixed it today. [Notification at Specific Time using BroadcastReceiver and AlarmManager](https://stackoverflow.com/questions/47420667/notification-at-specific-time-using-broadcastreceiver-and-alarmmanager) – Araz Mohammadnejad Nov 22 '17 at 14:01

2 Answers2

3

That's not the purpose of setWhen. It's only a UI thing, if setShowWhen is enabled this timestamp is shown at the notification.

For your purpose, I would suggest the android AlarmManager. An example can be found here

Christopher
  • 9,682
  • 7
  • 47
  • 76
1

setWhen() is used to show the time on the notification itself, not when the notification is shown.

The time on the top right of the notification is what setWhen() controls:

enter image description here

For showing the notification at a particular time, you can use the AlarmManager class. This has code to do the same.

Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40