2 * Copyright 2012 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.google
.android
.gcm
;
19 import static com
.google
.android
.gcm
.GCMConstants
.DEFAULT_INTENT_SERVICE_CLASS_NAME
;
21 import android
.app
.Activity
;
22 import android
.content
.BroadcastReceiver
;
23 import android
.content
.Context
;
24 import android
.content
.Intent
;
25 import android
.util
.Log
;
28 * {@link BroadcastReceiver} that receives GCM messages and delivers them to
29 * an application-specific {@link GCMBaseIntentService} subclass.
31 * By default, the {@link GCMBaseIntentService} class belongs to the application
32 * main package and is named
33 * {@link GCMConstants#DEFAULT_INTENT_SERVICE_CLASS_NAME}. To use a new class,
34 * the {@link #getGCMIntentServiceClassName(Context)} must be overridden.
36 public final class GCMBroadcastReceiver
extends BroadcastReceiver
{
38 private static final String TAG
= "GCMBroadcastReceiver";
39 private static boolean mReceiverSet
= false;
42 public final void onReceive(Context context
, Intent intent
) {
43 Log
.v(TAG
, "onReceive: " + intent
.getAction());
44 // do a one-time check if app is using a custom GCMBroadcastReceiver
47 String myClass
= getClass().getName();
48 if (!myClass
.equals(GCMBroadcastReceiver
.class.getName())) {
49 GCMRegistrar
.setRetryReceiverClassName(myClass
);
52 String className
= getGCMIntentServiceClassName(context
);
53 Log
.v(TAG
, "GCM IntentService class: " + className
);
54 // Delegates to the application-specific intent service.
55 GCMBaseIntentService
.runIntentInService(context
, intent
, className
);
56 setResult(Activity
.RESULT_OK
, null /* data */, null /* extra */);
60 * Gets the class name of the intent service that will handle GCM messages.
62 protected static String
getGCMIntentServiceClassName(final Context context
) {
63 return getDefaultIntentServiceClassName(context
);
67 * Gets the default class name of the intent service that will handle GCM
70 private static final String
getDefaultIntentServiceClassName(final Context context
) {
71 String className
= context
.getPackageName() +
72 DEFAULT_INTENT_SERVICE_CLASS_NAME
;