0

我正在使用Firebase将通知从服务器推送到特定的Android设备。我希望在收到通知时获得时间(设备的本地时间不是服务器时间)。我怎么弄到的?在Android中获取通知时间

因为我必须显示一个关于那个时间的计时器,所以这就是为什么当在该设备上收到通知时需要准确的时间。

回答

4

我认为,一个有效的办法是使用日历类在onMessageReceived()有点像这样:

Calendar.getInstance().getTimeInMillis(); 
+0

我想会有的方法任何内置。 ...但正如你所提到的使用onMessageReceived().....所以我把Millis的时间放在extras中,通过使用onMessageReceived()来获取intent。感谢您的帮助:) –

+0

此外,请记住通过您的应用程序服务器只发送** **数据有效载荷,或者您的onMessageReceived在应用程序处于后台或被杀时不会被触发,在这种情况下,跟踪您收到的时间会有点棘手通知。 –

+0

该死!你是对的@Nishant当应用程序关闭时,我无法获得时间。有什么建议么? –

1
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()); 
    } 
0

可以在onMessageReceived()方法使用这个火力点功能:

Long time = remoteMessage.getSentTime(); 

希望这有助于:)

+0

can如果这个答案对你有帮助,你是否接受? –