【发布时间】:2017-12-27 10:25:10
【问题描述】:
现在我知道,如果我发送数据消息,即使应用程序在后台,也会收到通知。但即使在发送数据消息之后,我也没有收到任何通知。
我正在使用 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
P.S.:当应用程序在前台时,收到通知就好了。
【问题讨论】:
-
感谢您的回复!但我只使用数据有效负载..问题也需要使用通知有效负载。此外,我无法在任何地方看到通知。
-
好的,澄清一下你想要
data和notifcation?还是只有一个? -
我想在应用程序在后台时收到通知...我真的很困惑...我看到了其他类似问题的答案,他们说我需要使用数据有效负载只有这样,当应用程序在后台时才会收到通知..你能帮忙吗?
标签: java android firebase firebase-notifications