I know the risk of doing this.
But right now I only want to send message from one device to another device without any server.
Here is what I'm try.
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;
}
But I'm seeing an error.
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)
I'm not able to send any message, I checked my firebase token all are correct and server key also then what is the problem. Can you please let me know about this what I'm missing.
And second thing is I see the build in method already there for sending some kind of messages. But it's not working. What is the purpose of this method can you please let me know about this
Here is the code of this method. Can you please explain it.
RemoteMessage message = new RemoteMessage.Builder(getRegistrationToken())
.setMessageId(incrementIdAndGet())
.addData("message", "Hello")
.build();
FirebaseMessaging.getInstance().send(message);
What is this ????
Update
in documentation there described how you can send firebase messages using http request. right here Build App Server Send Requests can I use this method to send request from android device. Because in this method server key is not required. Can some body let me know about this is it possible or not ?