简体   繁体   English

如何设置通知的时间和日期

[英]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 )但是System.currentTimeMillis()返回设备上的当前时间,但我想添加指定的时间,例如(星期五 15:00)

how can i do that ??我怎样才能做到这一点 ?? how can i set this time in milliseconds and load it up in .setWhen我如何以毫秒为单位设置这个时间并在.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.您应该使用 Java Calendar类来指定日期。 Instantiate a Calendar object using the getInstance() factory method.使用getInstance()工厂方法实例化Calendar对象。 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. Calendar包含不同的set()方法来指定日期,选择一个更适合您需要的方法并使用它来指定您需要的日期。 Then call the getTimeInMillis() method, which will return specified date in milliseconds.然后调用getTimeInMillis()方法,该方法将以毫秒为单位返回指定的日期。 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.正如@Deimos 在评论中已经提到的那样,'setWhen' 函数仅设置用于在通知中显示的特定通知的时间戳,它不是 'FIRE' 的时间戳或在特定时间点触发通知。 It is part of the Notification(.Builder) class that seems only to have support for creating a custom notification.它是 Notification(.Builder) 类的一部分,似乎只支持创建自定义通知。 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.发出通知,例如在星期日下午 1 点触发的通知,以提醒用户周末的某事可以在本地实现,例如在 AlarmManager 和 Broadcast 的帮助下,警报管理器根据给定的日期和时间或远程使用任何服务器实例触发通知像Android的FCM。 I hope this cleared any possiblly upcoming misunderstandings.我希望这消除了任何可能即将发生的误解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 Android 中的日期和时间选择器对话框设置的特定时间和日期设置通知 - How can I set a notification for specific time & date set by date & time picker dialog in Android 如何在我的自定义日期和时间中设置通知 - How to set notification in my custom date and time 如何设置用户选择的日期和时间的通知? - How to set notification on user selected date and time? Android,如何将闹钟服务设置为预定义的日期时间并运行通知 - Android, how to set alarm service to the predefined date time and run notification 如何准时设置通知 - How to set Notification on time 使用Azure Notification Hub时如何设置“安静时间”? - How can I set “Quiet Time” when using Azure Notification Hub? 如何在通知中显示日期和时间? - How to display date and time in notification? 如何在特定时间在android应用程序中设置通知? - How do I set notification in android application at specific time? 是否可以通过Firebase停止推送通知,或者可以设置过期通知时间? - Is there a way to stop a push notification with Firebase or can I set an expire notification time? 我如何在房间使用日期和时间 - How can I work with Date and Time at Room
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM