Reputation: 925
in the notification manager we use this method to set time and date to fire notification
.setWhen(System.currentTimeMillis());
but the System.currentTimeMillis()
get back the current time on device but i want to add specified time like ( Friday 15:00 )
how can i do that ?? how can i set this time in milliseconds and load it up in .setWhen
thanks in advance
this code i use
Notification.Builder builder =
new Notification.Builder(MyActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setTicker(“Notification”)
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE)
.setSound(
RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 0, 1);
Notification notification = builder.getNotification();
Upvotes: 9
Views: 19212
Reputation: 11
As @Deimos already mentioned in the comments, the 'setWhen' function only sets the timestamp for the certain notification for displaying in the notification, it is NOT the timestamp to 'FIRE' or trigger the notification at a specific point of time. It is part of the Notification(.Builder) class that seems only to have support for creating a custom notification. FIRING a notification e.g. notification triggered at sunday 1pm to remind the user of sth at the weekend can be realized e.g. locally with the help of AlarmManager and Broadcast and the Alarm Manager is firing the notification according to given date and time or remotely with any server instances like FCM for Android. I hope this cleared any possiblly upcoming misunderstandings.
Upvotes: 1
Reputation: 1686
import java.util.*;
...
builder.setWhen(Calendar.getInstance().getTimeInMillis() );
Upvotes: 2
Reputation: 40203
You should use the Java Calendar class to specify a date. Instantiate a Calendar
object using the getInstance()
factory method. By default, the instance will be initialized with the current date and time for the default time zone of your machine. Calendar
contains different set()
methods to specify a date, choose one that's more appropriate for your needs and use it to specify the date you need. Then call the getTimeInMillis()
method, which will return specified date in milliseconds. Hope this helps.
Upvotes: 9