我试图使用以下方法在Android中加载FCM通知总数,但它不是打印整个数据 - 它只是在以下数据打印。如何加载来自服务器的数据?
服务器有效载荷
JSONObject json = new JSONObject();
json.put("to", deviceToken.trim());
JSONObject data = new JSONObject();
data.put("Key-1", Message);
data.put("Key-2", Message1);
json.put("data", data);
JSONObject info = new JSONObject();
info.put("title", "EZBitex Exchange");
info.put("body", "EZBitex Exchange");
info.put("message", "hello user");
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
System.out.println("GCM Notification is sent successfully");
result = "succcess";
} catch (Exception e) {
e.printStackTrace();
result = "failure";
}
return result;
Android
public class FirebaseMessagingservice extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
System.out.println("NEW_TOKEN is---->"+s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
System.out.println("JSON_OBJECT is---->"+object.toString());
}
}
打印数据
JSON_OBJECT is---->{"Key-1":"Hello dude","Key-2":"welcome to Ezbitex exchange"}
作为 remoteMessage.getData()
提取数据有效载荷。相同的remoteMessage.getNotification()
提取通知有效载荷
我希望您正在寻找获取通知对象的代码。
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null){
Log.d(TAG, "Notification object:" + remoteMessage.getNotification());
//show notification in tray here - if required
Log.d(TAG, "Notification body:" + remoteMessage.getNotification().getBody());//will get "EZBitex Exchange"
Log.d(TAG, "Notification title:" + remoteMessage.getNotification().getTitle());//will get "EZBitex Exchange"
}
}