我知道这样做的风险。
但是现在我只想将消息从一台设备发送到另一台没有任何服务器的设备。
这是我要尝试的。
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 在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250) W / System.err:位于com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java) 在org.octabyte.zeem.InstantChat.SendMessage.openServerConnection(SendMessage.java:124) W / System.err:位于org.octabyte.zeem.InstantChat.SendMessage.doInBackground(SendMessage.java:74) 在org.octabyte.zeem.InstantChat.SendMessage.doInBackground(SendMessage.java:21) 在android.os.AsyncTask $ 2.call(AsyncTask.java:304) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) W / System.err:位于android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607) W / System.err:位于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消息。在这里Build App Server Send Requests,我可以使用这种方法从android设备发送请求。因为在这种方法中不需要服务器密钥。可以让我知道这是否可能吗?
答案 0 :(得分:1)
我对其进行测试并运行您的代码。问题出在postData
您是encoding
json
数据中,这可以解决问题。只需替换此行
String postData = URLEncoder.encode(root.toString(),"UTF-8");
// replace with
String postData = root.toString();