【发布时间】:2016-10-17 07:16:00
【问题描述】:
我正在使用 Firebase 将通知从服务器推送到特定的 android 设备。我想在收到通知时获取时间(设备的本地时间而不是服务器时间)。我怎样才能得到它?
因为我必须显示一个与该时间相关的计时器,所以我需要在该设备上收到通知时的确切时间。
【问题讨论】:
标签: android time firebase push-notification firebase-cloud-messaging
我正在使用 Firebase 将通知从服务器推送到特定的 android 设备。我想在收到通知时获取时间(设备的本地时间而不是服务器时间)。我怎样才能得到它?
因为我必须显示一个与该时间相关的计时器,所以我需要在该设备上收到通知时的确切时间。
【问题讨论】:
标签: android time firebase push-notification firebase-cloud-messaging
我认为一种有效的方法是在 onMessageReceived() 中使用 Calendar 类,有点像这样:
Calendar.getInstance().getTimeInMillis();
【讨论】:
你可以在onMessageReceived()方法中使用这个firebase函数:
Long time = remoteMessage.getSentTime();
希望这会有所帮助:)
【讨论】:
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
在扩展 FirebaseMessagingService 的服务类中写入这一行。 函数看起来像
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
Log.d("time",currentDateTimeString);
createNotification(remoteMessage.getNotification().getBody());
}
【讨论】: