Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401

后端 未结 3 1458
死守一世寂寞
死守一世寂寞 2020-11-30 14:37

I am trying to get Google Cloud Messaging (GCM) to work on Android with local device groups as described on https://developers.google.com/cloud-messaging/android/client-devi

3条回答
  •  天命终不由人
    2020-11-30 15:08

    Here's the recap, based on the correct answer by greywolf82. The correct code should follow these principles (error handling etc. has been stripped):

    ///////////////////////////////////////////////////////////////////////////
    // Working example on how to create a locally (client-side) managed      //
    // device group for Google Cloud Messaging.                              //
    //                                                                       //
    // Thanks to greywolf82 for adding the final piece.                      //
    // Please vote on his answer. Thank you!                                 //
    ///////////////////////////////////////////////////////////////////////////
    
    // Get token:
    String account = ACCOUNT_NAME; // E.g. "johndoe@gmail.com"
    String scope = "audience:server:client_id:" + WEB_APPLICATION_CLIENT_ID;
    String idToken = GoogleAuthUtil.getToken(context, account, scope);
    
    // Get registration id:
    InstanceID instanceID = InstanceID.getInstance(this);
    String registration_id = instanceID.getToken(
            getString(R.string.gcm_defaultSenderId),
            GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    
    // Set up HTTP connection:
    URL url = new URL("https://android.googleapis.com/gcm/googlenotification");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestProperty("project_id", NUMERICAL_PROJECT_ID);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Accept", "application/json");
    connection.connect();
    
    JSONObject requestBody = new JSONObject();
    requestBody.put("operation", "add");
    requestBody.put("notification_key_name", ACCOUNT_NAME); // You *must* use the email!
    requestBody.put("registration_ids",
        new JSONArray(Arrays.asList(new String[]{registration_id})));
    requestBody.put("id_token", idToken);
    
    // Submit request body
    OutputStream os = connection.getOutputStream();
    os.write(requestBody.toString().getBytes("UTF-8"));
    os.close();
    
    // connection.getResponseCode() is now 200  :-)
    // Now read the server response contents from connection.getInputStream()
    

    The submitted JSON to https://android.googleapis.com/gcm/notification looks something like this:

    {
      "operation": "add",
      "notification_key_name": "johndoe@gmail.com",
      "registration_ids": ["very long string here"],
      "id_token": "another very long string"
    }
    

    The response contents is:

    {
      "notification_key": "your notification key is here --- voilá!"
    }
    

提交回复
热议问题