1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.components.gcm_driver;
7 import android.content.Intent;
8 import android.os.Bundle;
10 import com.google.ipc.invalidation.external.client.contrib.MultiplexingGcmListener;
12 import org.chromium.base.ThreadUtils;
15 * Receives GCM registration events and messages rebroadcast by MultiplexingGcmListener.
17 public class GCMListener extends MultiplexingGcmListener.AbstractListener {
19 * Receiver for broadcasts by the multiplexed GCM service. It forwards them to
22 * This class is public so that it can be instantiated by the Android runtime.
24 public static class Receiver extends MultiplexingGcmListener.AbstractListener.Receiver {
26 protected Class<?> getServiceClass() {
27 return GCMListener.class;
31 private static final String TAG = "GCMListener";
33 // Used as a fallback until GCM always gives us the subtype.
34 // TODO(johnme): Remove this once it does.
35 static final String UNKNOWN_APP_ID = "push#https://www.gcmlistenerfake.com#0";
37 public GCMListener() {
42 protected void onRegistered(String registrationId) {
43 // Ignore this, since we register using GoogleCloudMessagingV2.
47 protected void onUnregistered(String registrationId) {
48 // Ignore this, since we register using GoogleCloudMessagingV2.
52 protected void onMessage(final Intent intent) {
53 final String bundleSubtype = "subtype";
54 final String bundleDataForPushApi = "data";
55 Bundle extras = intent.getExtras();
56 if (!extras.containsKey(bundleSubtype) && !extras.containsKey(bundleDataForPushApi)) {
57 // TODO(johnme): Once GCM always gives us the subtype, we'll be able to early-out if
58 // there is no subtype extra. For now we have to also allow messages without subtype
59 // if they have the data key which suggests they are for the Push API, but this is
60 // technically a layering violation as this code is for other consumers of GCM too.
63 final String appId = extras.containsKey(bundleSubtype) ? extras.getString(bundleSubtype)
65 ThreadUtils.runOnUiThread(new Runnable() {
66 @Override public void run() {
67 GCMDriver.onMessageReceived(getApplicationContext(), appId,
74 protected void onDeletedMessages(int total) {
75 ThreadUtils.runOnUiThread(new Runnable() {
76 @Override public void run() {
77 GCMDriver.onMessagesDeleted(getApplicationContext(), UNKNOWN_APP_ID);