确保使用Google FCM的App已经开启FCM服务并在代码中正确配置了FCM密钥。
在应用程序的build.gradle文件中,确保已经添加了Google服务插件依赖关系,该依赖关系将FCM库引入到应用程序中。
确保在FCM通知消息的有效负载中包含正确的时间戳。
以下是示例代码:
JSONObject notificationPayload = new JSONObject();
notificationPayload.put("title", "New Notification");
notificationPayload.put("body", "This is a sample notification");
notificationPayload.put("timestamp", System.currentTimeMillis());
JSONObject messagePayload = new JSONObject();
messagePayload.put("notification", notificationPayload);
// 构建FCM消息
Message message = Message.builder()
.setToken(getDeviceToken())
.putData("data", messagePayload.toString())
.build();
// 发送FCM消息
String response = FirebaseMessaging.getInstance().send(message);
在上述示例中,注意到FCM消息的有效负载“notificationPayload”对象包含一个“timestamp”字段,其中包含当前时间的时间戳(即System.currentTimeMillis())。这个时间戳将在显示通知时用于计算通知的实际时间和日期。确保在您的应用程序中正确设置这个时间戳将有助于确保正确的通知时间在应用程序中显示。