GCM-based group device management

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?

+1
source share
1 answer

I'm not sure if this can be done without a server API key. Error 401 indicates that some HTTP authorization header should be included if this URL is used to configure a group of devices.

My best suggestion is to keep the server API key well hidden using the client caching mechanism ( http://developer.android.com/training/articles/keystore.html ).

For more information on how to do all this with the SERVER_API key, see here: Google Cloud Messaging (GCM) with local groups of Android devices gives an HTTP 401 error code

Hope this helps. :-)

0
source

Source: https://habr.com/ru/post/1204201/


All Articles