我试图在我的应用中实施FCM通知。我已阅读FCM数据消息类型即使在应用程序处于后台时也会收到通知,所以我试图在onMessageRecieved
方法中实现这样的意外响应,如下所示:
{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消息代码:
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());
//
}
}
在这条日志消息中我得到的反应是如何从中获取价值的尝试如下:
int title=data.get("title");
获取空指针,因为它不是有效格式。在我的服务器端,我试图发布像这样的json格式:
{
"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"
}
不知道我犯了什么错误。谁能帮我?提前谢谢!
答案 0 :(得分:7)
获取标题:来自消息有效负载
使用:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
String title = remoteMessage.getData().get("title").toString();
}
修改强>
检查您的remoteMessage是否包含特定密钥:
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Log.d(TAG, "key, " + key + " value " + value);
}
答案 1 :(得分:2)
经过多次搜索,我找到了这个答案,它工作完美
在以下情况下,使firebase库调用onMessageReceived()
您不得在对Firebase API的请求中放入JSON密钥“ notification”,而应使用“ data”,请参见下文。
当您的应用程序在后台或被杀死时,以下消息不会调用您的onMessageReceived(),并且您无法自定义通知。
{
"to": "/topics/journal",
"notification": {
"title" : "title",
"text": "data!",
"icon": "ic_notification"
}
}
但是使用它会起作用
{
"to": "/topics/dev_journal",
"data": {
"text":"text",
"title":"",
"line1":"Journal",
"line2":"刊物"
}
}
基本上,消息与参数Data一起作为参数在Map的RemoteMessage中发送,然后您可以在onMessageReceived中管理通知,如此处的代码段所示。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
//you can get your text message here.
String text= data.get("text");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// optional, this is to make beautiful icon
.setLargeIcon(BitmapFactory.decodeResource(
getResources(), R.mipmap.ic_launcher))
.setSmallIcon(smallIcon) //mandatory
.......
/*You can read more on notification here:
https://developer.android.com/training/notify-user/build-notification.html
https://www.youtube.com/watch?v=-iog_fmm6mE
*/
}
参考:How to handle notification when app in background in Firebase
答案 2 :(得分:0)
您的"title"
参数中还有一个额外的 空格字符 :
"title ":"1"
很难看,因为它只是一个空间。它应该是:
"title":"1"
您注意获取任何值的原因是,从技术上讲,发送的密钥为"title "
(带空格),而在您的客户端代码中,您只使用&#34 ; title
&#34; (没有空格)。
删除额外空格后,您应该能够正确接收它。
答案 3 :(得分:0)
FCM消息有两种类型:
1)通知消息
2)数据消息
通知消息结构:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
为了从通知有效内容/消息中获取数据:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message From " + remoteMessage.getFrom()); //sender ID
Log.d(TAG, "Notification Title " + remoteMessage.getNotification().getTitle()); //notification title
Log.d(TAG, "Notification Body " + remoteMessage.getNotification().getBody()); //notification body
}
}
数据消息结构:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
为了从数据有效载荷/消息中获取数据:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data: " + remoteMessage.getData()); //Whole data
Log.d(TAG, "Key Data : " + remoteMessage.getData().get("key").toString()); //Get specific key data
}
}