我正在尝试在我的应用中实现 FCM 通知.我已阅读 FCM 数据消息类型即使在应用程序处于后台时也会收到通知,因此我尝试在 onMessageRecieved
方法中实现该通知,但收到如下意外响应:
I'm trying to implement FCM notification in my app. I have read FCM data message type will receive notification even when app is in background so am trying to implementing that in onMessageRecieved
method am getting unexpected response like this:
{title =2, message={"Status":"UNASSIGNED","CompanyName":"gd","LastModifiedDateTime":"2017-04-25 18:59:41","IsPartRequired":false,"ProblemCategory":"CONFIGURATION","IsGeneralClaim":false,"RegistrationID":1057,"IncidentCode":"INS/2017/04/25-0010","StatusID":0,"CreatedDateTime":"2017-04-25 18:59:41","IsInstallationCall":false}}
不知道如何解析这个从标题和消息中获取单独的值让我发布我的 firebase 消息代码:
Don't know how to parse this get separate value from title and message let me post my firebase message code:
public class FireBaseMessage extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String,String> data = remoteMessage.getData();
Log.d(TAG, "From: " + data.toString());
//
}
}
在此日志消息中,我得到了这样的响应,如何从中获取价值是这样的尝试:
In this log message am getting response like that how to get value from that is try like:
int title=data.get("title");
获取空指针,因为它的格式无效.在我的服务器端,我试图发布这样的 json 格式:
getting null pointer as this is not in valid format. In my server side i have am trying to post json format like this:
{
"to":"es_OToDkj00:APA91bFqxbVMAaXy5fPtDbNVAkIwyVrPCmfGci2otHZPvdRoXPv-oDdjgtLR92Nqe8w6f57nCVceLbc3_zBWsInG9g1Pfdp3LvsMKyuaiYps0L1y3tn0N0XbzGseEI6jyiqs1r-sT9lb",
"data":{
"message":{
"RegistrationID":1057,
"IncidentCode":"INS/2017/04/25-0010",
"CompanyName":"ABM INFOTECH",
"StatusID":5,
"Status":"ASSIGNED",
"CreatedDateTime":"2017-04-25T12:03:45",
"LastModifiedDateTime":"2017-04-25T18:59:41",
"ProblemCategory":"CONFIGURATION",
"IsPartRequired":false,
"IsInstallationCall":false,
"IsGeneralClaim":false
},
"title ":"1"
}
不知道我在哪里犯了错误.谁能帮我?提前致谢!
Don't know where I'm making a mistake. Can anyone help me? Thanks in advance!
To get Title: from message payload
To get the Title: from message payload
使用:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
//In case when notification was send in "notification" parameter we need to check wheather data is null or not.
if (remoteMessage.getData()!=null && remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String title = remoteMessage.getData().get("title").toString();
}
}
编辑
检查你的 remoteMessage 是否包含特定的键:
check if your remoteMessage contain the specific key:
if (remoteMessage.getData()!=null){
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d(TAG, "key, " + key + " value " + value);
}}
这篇关于如何从 Android 中的 FCM 数据消息中获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!