FCM Api was updated to v1, and now u can't pass key=<API_KEY> (once generated from FCM Console) header as before, now u should generate it by SDK com.google.api-client and there u should pass mystery SCOPES inside createScoped() method. Here is a lot of examples about it - without information about scopes. But what is this? where to get it? I can't to find any information about it. Please help me
The scope is list of String. I have tried several endpoints from https://developers.google.com/identity/protocols/googlescopes and worked.
"https://www.googleapis.com/auth/firebase.readonly"
val googleCredential = GoogleCredential.fromStream("yourjson.json")
.createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/firebase.readonly"))
googleCredential.refreshToken()
return googleCredential.accessToken
This way of getting access token is not working I tried.But u can try a direct http request.
public static void main(String args[]) throws IOException {
public final static String AUTH_KEY_FCM = "server key";
public final static String API_URL_FCM =
"https://fcm.googleapis.com/fcm/send";
// Method to send Notifications from server to client end.
// userDeviceIdKey is the device id you will query from your database
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + authKey);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to",
"Device key");
JSONObject info = new JSONObject();
info.put("title", "Demo"); // Notification title
info.put("body", "Hello Test notification"); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
}
}
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