I'm having problems adding the GCM registration ID to the client side of the device group in the Android app. I followed all the instructions https://developers.google.com/cloud-messaging/android/client-device-group , but continue to receive an HTTP 401 response. I found the following messages, but no one answered ...
I successfully receive an authentication token from GoogleSignInApi and the method specified in Google instructions, but both return a 401 response. I made sure I use the client ID for the web application in my Google Developer Console and still no luck. Here is my code snippet ...
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", getString(R.string.gcm_defaultSenderId)); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setRequestMethod("POST"); con.connect(); String accountName = getAccount(); //Initialize the scope using the client ID you got from the Console. final String scope = "audience:server:client_id:" + "MY_WEB_APP_CLIENT_ID"; String idToken = ""; try { idToken = GoogleAuthUtil.getToken(this, sharedPref.getString("googleEmail", ""), scope); } catch (Exception e) { e.printStackTrace(); } // HTTP request JSONObject data = new JSONObject(); data.put("operation", "add"); data.put("notification_key_name", "my_group_name"); 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(); int responseCode = con.getResponseCode();
I don't think this is important for this HTTP request, but I also provided the correct project and client ID (android), which are stored in my google-services.json. Has anyone had success managing device client groups? If so, what is different in your code from yours?
source share