Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

Managing Device Groups on Android Client Apps

Managing device groups on the client is useful for cases where a server is unavailable. To create a device group on the client, the device must have at least one Google account. Note that the process for creating a notification key on the client is significantly different from the server-side process described in Device Group Messaging.

To create a device group on the client:

Obtain a Client ID

  1. Open your project in the Google Developers Console.
  2. From the menu at top left, select APIs & Services and then Credentials.
  3. Click New credentials and select OAuth Client ID.
  4. In the Create Client ID dialog, select Web Application as the application type, and click Create.
  5. Copy the value displayed for the Client ID. This client ID represents a Google account "scope" that you will use to generate an idToken.

Validate the Google account on the device

Once you've obtained a client ID from Google Developers Console, check the device for the presence of a Google account.

// This snippet takes the simple approach of using the first returned Google account,
// but you can pick any Google account on the device.
public String getAccount() {
    Account[] accounts = AccountManager.get(getActivity()).
        getAccountsByType("com.google");
    if (accounts.length == 0) {
        return null;
    }
    return accounts[0].name;
}

Get an authentication token

Next, get an authentication token (idToken) by using the GoogleAuthUtil class. For example:

String accountName = getAccount();

// Initialize the scope using the client ID you got from the Console.
final String scope = "audience:server:client_id:"
        + "1262xxx48712-9qs6n32447mcj9dirtnkyrejt82saa52.apps.googleusercontent.com";
String idToken = null;
try {
    idToken = GoogleAuthUtil.getToken(context, accountName, scope);
} catch (Exception e) {
    log("exception while getting idToken: " + e);
}
...

Add or remove devices from groups

Build an HTTP POST request to https://android.googleapis.com/gcm/googlenotification to add/remove registration tokens to/from a group. The request header needs to have project_id set to the sender ID and Content-Type set to JSON.

Add to group

An add operation requires the following keys: operation set to add, id_token set to the idToken obtained above, notification_key_name and registration_ids. The userEmail variable, as shown below, can be derived from the value of accounts[0].name. The client is authorized to manage only groups mapped to this email/account.

public String addtNotificationKey(
        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");

}

A successful operation returns a notification_key. Save this notification_key and the corresponding notification_key_name to use in subsequent operations.

Remove from group

A remove operation requires the following keys: operation set to remove, id_token set to the idToken obtained above, notification_key_name and registration_ids.

// HTTP request
JSONObject data = new JSONObject();
data.put("operation", "remove");
data.put("notification_key_name", userEmail);
data.put("registration_ids", new JSONArray(Arrays.asList(registrationId)));
data.put("id_token", idToken);

Send feedback about...

Cloud Messaging
Cloud Messaging