简体   繁体   中英

how can i set time and date for notification

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();

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.

import java.util.*;
...
builder.setWhen(Calendar.getInstance().getTimeInMillis() );

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 eg notification triggered at sunday 1pm to remind the user of sth at the weekend can be realized eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM