Firebase:来自 Android 应用程序 returns 的 HTTP POSTing 通知成功但没有任何反应
Firebase: HTTP POSTing Notification from Android App returns success but nothing happens
我正在尝试 post 直接从我的应用程序而不是使用 Firebase 控制台发送通知。
我写的代码是这样的:
URL url= null;
HttpURLConnection client = null;
try{
url = new URL("https://fcm.googleapis.com/fcm/send");
client = (HttpURLConnection) url.openConnection();
client.setDoOutput(true);
client.setRequestMethod("POST");
client.setRequestProperty("Content-Type", "application/json");
client.setRequestProperty("Accept", "application/json");
client.setRequestProperty("Authorization", "key=<here my server key from firebase site");
client.setRequestProperty("project_id", "<here my sender ID from firebase site");
client.connect();
JSONObject payload = new JSONObject();
payload.put("body",news.text);
payload.put("title",news.title);
JSONObject notif = new JSONObject();
notif.put("to", "/topics/MyFirstTopic");
notif.put("notification", payload);
OutputStream outputPost = client.getOutputStream();
outputPost.write(notif.toString().getBytes("UTF-8"));
outputPost.flush();
outputPost.close();
// Read the response into a string
InputStream is = client.getInputStream();
String responseString = new Scanner(is, "UTF-8").useDelimiter("\A").next();
is.close();
}
catch(Exception e) {
if (e.getMessage() != null) {
Log.e("SEND NOTIF TO FB", e.getMessage());
}
else {
Log.e("SEND NOTIF TO FB", e.toString());
}
}
finally {
if(client!=null) client.disconnect();
}
代码运行没有错误并且响应(在 responseString 中)显然是正确的:
{
"message_id": "123456789..."
}
但是在 Firebase 控制台中(在通知页面中)我看不到 posted 通知,两个设备都没有收到它。
我错过了什么??
注意:为了简洁起见,我没有post这里的权限设置和威胁通知代码(onMessageReceive...),但我可以保证它适用于从 Firebase 控制台发送的通知。
Firebase 通知控制台仅显示从控制台本身发送的消息。它不显示您通过 Firebase Cloud Messaging API.
发送的消息
我正在尝试 post 直接从我的应用程序而不是使用 Firebase 控制台发送通知。 我写的代码是这样的:
URL url= null;
HttpURLConnection client = null;
try{
url = new URL("https://fcm.googleapis.com/fcm/send");
client = (HttpURLConnection) url.openConnection();
client.setDoOutput(true);
client.setRequestMethod("POST");
client.setRequestProperty("Content-Type", "application/json");
client.setRequestProperty("Accept", "application/json");
client.setRequestProperty("Authorization", "key=<here my server key from firebase site");
client.setRequestProperty("project_id", "<here my sender ID from firebase site");
client.connect();
JSONObject payload = new JSONObject();
payload.put("body",news.text);
payload.put("title",news.title);
JSONObject notif = new JSONObject();
notif.put("to", "/topics/MyFirstTopic");
notif.put("notification", payload);
OutputStream outputPost = client.getOutputStream();
outputPost.write(notif.toString().getBytes("UTF-8"));
outputPost.flush();
outputPost.close();
// Read the response into a string
InputStream is = client.getInputStream();
String responseString = new Scanner(is, "UTF-8").useDelimiter("\A").next();
is.close();
}
catch(Exception e) {
if (e.getMessage() != null) {
Log.e("SEND NOTIF TO FB", e.getMessage());
}
else {
Log.e("SEND NOTIF TO FB", e.toString());
}
}
finally {
if(client!=null) client.disconnect();
}
代码运行没有错误并且响应(在 responseString 中)显然是正确的:
{
"message_id": "123456789..."
}
但是在 Firebase 控制台中(在通知页面中)我看不到 posted 通知,两个设备都没有收到它。
我错过了什么??
注意:为了简洁起见,我没有post这里的权限设置和威胁通知代码(onMessageReceive...),但我可以保证它适用于从 Firebase 控制台发送的通知。
Firebase 通知控制台仅显示从控制台本身发送的消息。它不显示您通过 Firebase Cloud Messaging API.
发送的消息