Reputation: 93
I try to create a group of devices in Firebase Cloud Messaging and I got an ioexception "https://android.googleapis.com/gcm/googlenotification". I have several questions about it:
Code:
public String addNotificationKey(
String senderId, String userEmail, String registrationId, String idToken)
throws IOException, JSONException {
URL url = new URL("https://android.googleapis.com/gcm/googlenotification");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
// HTTP request header
con.setRequestProperty("project_id", senderId);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");
con.connect();
// HTTP request
JSONObject data = new JSONObject();
data.put("operation", "add");
data.put("notification_key_name", userEmail);
data.put("registration_ids", new JSONArray(Arrays.asList(registrationId)));
data.put("id_token", idToken);
OutputStream os = con.getOutputStream();
os.write(data.toString().getBytes("UTF-8"));
os.close();
// Read the response into a string
InputStream is = con.getInputStream();
String responseString = new Scanner(is, "UTF-8").useDelimiter("\\A").next();
is.close();
// Parse the JSON string and return the notification key
JSONObject response = new JSONObject(responseString);
return response.getString("notification_key");
}
Upvotes: 0
Views: 236
Reputation: 37778
- What I need to put in fields: senderId, registrationId, idToken?
See their definitions in Credentials.
Sender ID can be found in your Firebase Console. Go to Project Settings, then Cloud Messaging tab.
Registration token is generated on your client app side. See the corresponding setup documentation depending on the client app type here.
idToken
is (AFAIK) only used on the client app side. See the (Android) documentation here.
- How I change this part of code to create a group and not add to group?
Change
data.put("operation", "add");
to
data.put("operation", "create");
- Where I need to put "authorization", "key=AIzaS..."?
See Puf's answer.
Upvotes: 0
Reputation: 598817
For #3:
con.setRequestProperty("Authorization", "key=AIzaS...");
Upvotes: 1