我试图在应用程序被杀或通知后获取通知中的数据是我所做的: php方面:
$msg = array
(
'body' => $msg,
'title' => $title,
'click_action' => "OPEN_ACTIVITY_1"
);
$fields = array
(
'to' => $token,
'notification' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
在我的luncher活动中:
if(getIntent().getExtras()!=null){
Strig msg = getIntent().getExtras().getString("body");
String title =getIntent().getExtras().getString("title");
Log.i(TAG,"Message "+msg);
Log.i(TAG,"Title " +title);
}
它为msg和title返回null 这是onMessageReceived()
的代码public void onMessageReceived(RemoteMessage remoteMessage) {
clickAction= remoteMessage.getNotification().getClickAction();
NotificationHelper h = new NotificationHelper(this);
Map<String,String >data = remoteMessage.getData();
title=data.get("title");
msg=data.get("body");
Log.i(TAG, "onMessageReceived: "+clickAction);
h.createNotificationWithId(msg,title,clickAction);
}
答案 0 :(得分:0)
您面临的问题与JSON有效负载的结构有关。
有关更多示例,请参阅this question here。
结果是如果你有'通知'键和对象值,你将无法在后台处理onMessageReceived。
//Will not work in your situation
{
"notification":{
"title": "hello",
"body": "this is body"
},
"to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
}
虽然这个会起作用,因为通知不存在,但是“数据”键存在。
//This will work for your situation
{
"data":{
"title": "hello",
"body": "this is body"
},
"to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
}
另外一件事,如果您同时拥有'通知'和'数据'键,它将默认为第一个示例,您将无法在onMessageReceived中手动处理它