在Android中无需服务器即可将FCM消息从一台设备发送到另一台设备



我知道这样做的风险。

但是现在我只想在没有任何服务器的情况下将消息从一台设备发送到另一台设备。

这是我正在尝试的。

JSONObject root = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("body", messageText);
notification.put("title", username);
JSONObject data = new JSONObject();
data.put("senderToken", senderToken);
data.put("messageId", messageId);
root.put("notification", notification);
root.put("data", data);
root.put("to", receiverToken);
String postData = URLEncoder.encode(root.toString(),"UTF-8");
return openServerConnection(postData);
private String openServerConnection(String postData){
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Content-Type","application/json");
httpURLConnection.setRequestProperty("Authorization", "key=AAAAPQ4yGQM:APA9....1cW");
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter( new OutputStreamWriter(outputStream, "UTF-8") );
bufferedWriter.write(postData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream, "iso-8859-1") );
StringBuilder result = new StringBuilder();
String line = "";
while( (line = bufferedReader.readLine()) != null ){
result.append(line);
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

但是我看到了一个错误。

W/System.err: java.io.FileNotFoundException: https://fcm.googleapis.com/fcm/send at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250( W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210( at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java( at org.octabyte.zeem.InstantChat.SendMessage.openServerConnection(SendMessage.java:124( W/System.err: at org.octabyte.zeem.InstantChat.SendMessage.doInBackground(SendMessage.java:74( at org.octabyte.zeem.InstantChat.SendMessage.doInBackground(SendMessage.java:21( at android.os.AsyncTask$2.call(AsyncTask.java:304( at java.util.concurrent.FutureTask.run(FutureTask.java:237( W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243( at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133( at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607( W/System.err: at java.lang.Thread.run(Thread.java:761(

我无法发送任何消息,我检查了我的 firebase 令牌都是正确的,服务器密钥也是什么问题。你能告诉我我错过了什么吗?

第二件事是我看到内置方法已经存在用于发送某种消息。但它不起作用。这种方法的目的是什么,你能告诉我这个吗

这是此方法的代码。你能解释一下吗?

RemoteMessage message = new RemoteMessage.Builder(getRegistrationToken())
.setMessageId(incrementIdAndGet())
.addData("message", "Hello")
.build();

FirebaseMessaging.getInstance((.send(message(;

这是怎麽????

更新

在文档中,介绍了如何使用 HTTP 请求发送 Firebase 消息。 就在这里 构建应用程序服务器 发送请求 我可以使用此方法从安卓设备发送请求。因为在这种方法中不需要服务器密钥。有人可以让我知道这可能吗?

我测试它并运行您的代码。 问题出在postData您正在encodingjson数据,这是问题所在。只需替换此行

String postData = URLEncoder.encode(root.toString(),"UTF-8");
// replace with
String postData = root.toString();

相关内容

  • 没有找到相关文章

最新更新