Gui*_*ido 6 android firebase firebase-cloud-messaging
我正在尝试将我的服务器上的通知发送到我的Android设备.我正在使用Firebase Cloud Messaging发送通知.我可以通过Firebase控制台发送通知,然后在手机上收到消息.但是,我正在尝试通过我的服务器发送消息,但该消息尚未生效.
我执行下面的代码时得到以下响应:
"{\" MESSAGE_ID\":58934758934758936346437}"
当我们在Firebase文档中查看Firebase的文档时,我们可以看到接收message_id意味着该消息已成功发送.我虽然没有在手机上收到它.
我确实订阅了应用程序到正确的主题.
我正在运行以下代码:
private void test(String topic) {
try {
//Setup request
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection hc = (HttpURLConnection) url.openConnection();
hc.setDoOutput(true);
hc.setDoInput(true);
//Set request params
String message = "{\"to\": \"/topics/" + topic + "\"}";
hc.addRequestProperty("Content-Type", "application/json");
hc.addRequestProperty("Authorization", "key=SECRET");
DataOutputStream dos = new DataOutputStream(hc.getOutputStream());
dos.writeBytes(message);
dos.close();
//Get Response
InputStream is = hc.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();
label.setText("RESPONSE: " + response.toString());
} catch (Exception ex) {
label.setText("Er ging iets mis");
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
您的有效负载不包含任何消息。
String message = "{\"to\": \"/topics/" + topic + "\"}";
Run Code Online (Sandbox Code Playgroud)
它只包含收件人 ( to),但没有实际的消息。发送notification或data消息有效负载。像这样的东西:
String message = "{\"to\": \"/topics/" + topic + "\",
\"notification\" : {
\"title\" : \"sample title\",
\"body\" : \"sample body\"
}
}";
Run Code Online (Sandbox Code Playgroud)
请参阅此处notification和 的可用参数,并注意这两个消息负载的处理方式不同。有关更多详细信息,请参阅Android 文档中的接收消息。data
| 归档时间: |
|
| 查看次数: |
6083 次 |
| 最近记录: |