Java 从android中的FCM获取自定义负载
我必须阅读下面提到的FCM推送通知键Java 从android中的FCM获取自定义负载,java,android,push-notification,firebase-cloud-messaging,Java,Android,Push Notification,Firebase Cloud Messaging,我必须阅读下面提到的FCM推送通知键 { "from_id": "", "data": { "sender_id": "15", "receiver_id": "42", "sender_name": "Addy", "alert": "Addy sent you a message.", "notification_type": "message", "m
{
"from_id": "",
"data": {
"sender_id": "15",
"receiver_id": "42",
"sender_name": "Addy",
"alert": "Addy sent you a message.",
"notification_type": "message",
"message": "testing"
}
}
我正在使用
public void onMessageReceived(RemoteMessage remoteMessage) {
String title=remoteMessage.getData().get("sender_name");
sendNotification(title, "");
}
问题是title=remoteMessage.getData.get(“发件人名称”);返回空值
,当我在浏览器中查看时,它会显示数据存在于发送者名称
中 你能试试这个吗
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
虽然我不能解决它,因为我已经开始与firebase,但我可以告诉你在哪里找到一个解决方案,因为它帮助了我很多。这里@SameerKhan1406我正在尝试这个JSONObject json=newjsonobject(remoteMessage.getData().toString());String title=json.getString(“发送者名称”);但我收到了JSONException。“发件人名称没有值”。但在logcat中,我可以看到数据负载的值:tag.getJSON对象作为响应,而不是获取单个数据,然后使用json.getString(“发送者名称”);分享你的代码我明天会帮你的好吗?
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}