I've already tried the following command in Curl to send a notification using the Firebase REST Api and it works:
curl -X POST --header "Authorization: key=AIza...iD9wk" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"to\": \"cAhmJfN...bNau9z\"}"
Now that I'm trying to do the same in Java, I couldn't find an easy way to do the same and nothing that I've tried triggers the notification in my mobile endpoint.
This is my closest approach:
try {
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("https://fcm.googleapis.com/fcm/send").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Authorization: key", "AIza...iD9wk");
httpcon.setRequestMethod("POST");
httpcon.connect();
System.out.println("Connected!");
byte[] outputBytes = "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"to\": \"cAhmJfN...bNau9z\"}".getBytes("UTF-8");
OutputStream os = httpcon.getOutputStream();
os.write(outputBytes);
os.close();
// Reading response
InputStream input = httpcon.getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
System.out.println("Http POST request sent!");
} catch (IOException e) {
e.printStackTrace();
}
But then I get:
java.io.IOException: Server returned HTTP response code: 401 for URL: https://fcm.googleapis.com/fcm/send
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at httpclient.test.MyHttpClientPost.sendNotification(MyHttpClientPost.java:131)
at httpclient.test.MyHttpClientPost.main(MyHttpClientPost.java:26)
The default location of the fcmjava.properties is System.getProperty ("user.home") + "/.fcmjava/fcmjava.properties". The file has to contain the FCM API Endpoint and the API Key:
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. FcmJava is a Java library for working with the Firebase Cloud Messaging (FCM) API.
It is essential to realize that the RESTful API methods are not returning the objects, but JSON representations of a Response object deserialized to a JSON string. A Response is an abstract class provided by the JAX-RS specification.
FcmJava uses Apache HttpClient for making requests to the Firebase Cloud Messaging server. So in order to configure a proxy for the HTTP requests, you can configure the HttpClientBuilder used in FcmJava. This is done by instantiating the DefaultHttpClient with your configured HttpClientBuilder.
401
means unauthorized, so no valid Authorization
header was sent.
And this line:
httpcon.setRequestProperty("Authorization: key", "AIza...iD9wk");
Is not equivalent to -H "Authorization: key=AIza...iD9wk"
. The first argument should be the header name, which is Authorization
:
httpcon.setRequestProperty("Authorization", "key=AIza...iD9wk");
In conclusion, you misunderstood how HTTP header is formatted. Basically the header name and value are separated by :
not =
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With