现在我知道,如果我发送数据消息,即使该应用程序在后台,通知也会到来。但是即使发送数据消息后,我也没有收到任何通知。
我将Postman用于测试目的,这是我的Postman请求正文:
{
"to" : (device token),
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"content_available" : true,
"priority" : "high",
"sound": "default",
"time_to_live":"2419200"
}
}
这是我的
onMessageReceived()
功能:public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String,String> data=remoteMessage.getData();
String title=data.get("title");
String body=data.get("body");
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(title,body);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
这就是我的
sendPushNotification()
函数的样子:private void sendPushNotification(String title,String message) {
try {
String imageUrl="";
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
if(imageUrl.equals("null")){
mNotificationManager.showSmallNotification(title, message, intent);
mNotificationManager.playNotificationSound();
}else{
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
mNotificationManager.playNotificationSound();
}
} catch (Exception e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
}
}
我在堆栈溢出时关注此问题:How to handle notification when app in background in Firebase
附注:当应用程序处于前台状态时,可以很好地接收通知。
最佳答案
正如您在我的帖子中看到的那样,对于特定ID,应使用"registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
键和值而不是”to”
。这是针对主题