【问题标题】:Get Notification received time in Android在Android中获取通知接收时间
【发布时间】:2016-10-17 07:16:00
【问题描述】:

我正在使用 Firebase 将通知从服务器推送到特定的 android 设备。我想在收到通知时获取时间(设备的本地时间而不是服务器时间)。我怎样才能得到它?

因为我必须显示一个与该时间相关的计时器,所以我需要在该设备上收到通知时的确切时间。

【问题讨论】:

    标签: android time firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    我认为一种有效的方法是在 onMessageReceived() 中使用 Calendar 类,有点像这样:

    Calendar.getInstance().getTimeInMillis();
    

    【讨论】:

    • 我认为会有任何内置方法......但正如你提到的使用 onMessageReceived()......所以我把 Millis 时间放在额外的内容中,通过使用 onMessageReceived()。感谢您的帮助:)
    • 另外,请记住通过您的应用服务器发送 only DATA 有效负载,否则当应用处于后台或被杀死时,您的 onMessageReceived 将不会被触发,在这种情况下,它会有点棘手跟踪您收到通知的时间。
    • 该死!你是对的@Nishant 我无法在应用程序关闭时获得时间。有什么建议吗?
    • 只要确保你的 json 有效载荷只发送 DATA 到 fcm 服务器,你就可以开始了。
    • { "notification":{ "body": "Notification Message", "title": "Notification Title", "sound": "default", "priority": "high", "click_action ": "ACTIVITY_MAIN_MAP" }, "data":{ "id": 2, "missedRequests": 5 }, "to": "fcmkey" } 这就是我要发送的内容....需要修改吗?
    【解决方案2】:

    你可以在onMessageReceived()方法中使用这个firebase函数:

    Long time = remoteMessage.getSentTime();
    

    希望这会有所帮助:)

    【讨论】:

    • 如果这个答案对你有帮助,你能接受吗?
    • 对于 kotlin,它将是 remoteMessage.sentTime
    【解决方案3】:
    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());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多