I'm trying to test out Firebase Cloud messaging in java and used the following code
String url = "https://fcm.googleapis.com/fcm/send";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "key="+ "<Server-Key>");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setAllowUserInteraction(false);
JSONObject infoJson = new JSONObject();
infoJson.put("title", title);
infoJson.put("body", notificationMsg);
JSONObject infoJson2 = new JSONObject();
infoJson2.put("userId", userId);
infoJson2.put("title",title);
infoJson2.put("body", notificationMsg);
infoJson2.put("notificationType", notificationType);
infoJson2.put("badge", String.valueOf(unreadNotificationCount));
JSONObject json = new JSONObject();
json.put("to",clientDeviceToken.trim());
json.put("priority","high");
json.put("content_available",true);
json.put("notification", infoJson);
json.put("data", infoJson2);
if (json != null) {
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//send the json as body of the request
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(json.toString());
wr.flush();
//Connect to the server
con.connect();
int status = con.getResponseCode();
if( status == 200 ){
//SUCCESS message
BufferedReader reader = new BufferedReader(new
InputStreamReader(con.getInputStream()));
System.out.println("Android Notification Response : " + reader.readLine());
}else if(status == 401){
//client side error
System.out.println("Notification Response : TokenId : " + clientDeviceToken + " Error occurred :");
}else if(status == 501){
//server side error
System.out.println("Notification Response : [ errorCode=ServerError ] TokenId : " + clientDeviceToken);
}else if( status == 503){
//server side error
System.out.println("Notification Response : FCM Service is Unavailable TokenId : " + clientDeviceToken);
}
my server key is correct i tested the same json by using postman and it is working. i searched may website all is saying the above code is true, I investigated the reasons for this, and tried it after regenerating new server key, but the error remains constant.
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
token_access: 7d29bdce3f93bc35200aef148c6371f264feff54
Content-Type: application/json
Authorization: key=<Server Key>
Content-Length: 357
{"notification":{"title":"Jebosse","body":"Test"},"content_available":true,
"data":{"badge":"1","notificationType":"BR","title":"Jebosse","body":"test ","userId":141},"to":"ee1jP3k7Twmm0AsBTjRpf-:APA91bFd3_z1EQK4t4gvyOERGVMbv-vrdNTr-Z5qaTtoWpXKhIDlo_42gCWj8A1ZJeAQAP1aKxYBpliKUSG7thbHM6JhdXU7g3ubYDB_7MMasyAKcQs8BMqx-xsHxpCEpLzUj2ca3E1h","priority":"high"}
i'am stuck. can any one help me . ?