GCMRegistrar | Android Developers

Quicknav Quicknav
Links

- Google Play Developer Console
- Android Developers Blog
- About Android

Android Sites

- Android.com
- Android Developers
- Android Open Source Project

Languages
close

- - Get Started
  - Style
  - Patterns
  - Building Blocks
  - Downloads
  - Videos
- - Training
  - API Guides
  - Reference
  - Tools
    - Get the SDK
  - Google Services
- - Google Play
  - Publishing
  - Promoting
  - App Quality
  - Spotlight
  - Open Distribution

## Results

Loading...
Summary: Constants | Methods | Inherited Methods | [Expand All]
public final class

# GCMRegistrar

extends Object

java.lang.Object
↳ | com.google.android.gcm.GCMRegistrar

## Class Overview

Utilities for device registration.

Note: this class uses a private `SharedPreferences` object to keep track of the registration token.

## Summary

Constants
long | DEFAULT_ON_SERVER_LIFESPAN_MS | Default lifespan (7 days) of the `isRegisteredOnServer(Context)` flag until it is considered expired.

Public Methods
static void | checkDevice(Context context) Checks if the device has the proper dependencies installed.
static void | checkManifest(Context context) Checks that the application manifest is properly configured.
static long | getRegisterOnServerLifespan(Context context) Gets how long (in milliseconds) the `isRegistered(Context)` property is valid.
static String | getRegistrationId(Context context) Gets the current registration id for application on GCM service.
static boolean | isRegistered(Context context) Checks whether the application was successfully registered on GCM service.
static boolean | isRegisteredOnServer(Context context) Checks whether the device was successfully registered in the server side, as set by `setRegisteredOnServer(Context, boolean)`.
synchronized static void | onDestroy(Context context) Clear internal resources.
static void | register(Context context, String... senderIds) Initiate messaging registration for the current application.
static void | setRegisterOnServerLifespan(Context context, long lifespan) Sets how long (in milliseconds) the `isRegistered(Context)` flag is valid.
static void | setRegisteredOnServer(Context context, boolean flag) Sets whether the device was successfully registered in the server side.
static void | unregister(Context context) Unregister the application.

[Expand] Inherited Methods
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)

## Constants

#### public static final long DEFAULT_ON_SERVER_LIFESPAN_MS

Default lifespan (7 days) of the `isRegisteredOnServer(Context)` flag until it is considered expired.

Constant Value: 604800000 (0x00000000240c8400)

## Public Methods

#### public static void checkDevice (Context context)

Checks if the device has the proper dependencies installed.

This method should be called when the application starts to verify that the device supports GCM.

##### Parameters

context | application context.

##### Throws

UnsupportedOperationException | if the device does not support GCM.

#### public static void checkManifest (Context context)

Checks that the application manifest is properly configured.

A proper configuration means:

1. It creates a custom permission called `PACKAGE_NAME.permission.C2D_MESSAGE`.
2. It defines at least one `BroadcastReceiver` with category `PACKAGE_NAME`.
3. The `BroadcastReceiver`(s) uses the `com.google.android.gcm.GCMConstants.PERMISSION_GCM_INTENTS` permission.
4. The `BroadcastReceiver`(s) handles the 2 GCM intents (`com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_MESSAGE` and `com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK`).

...where `PACKAGE_NAME` is the application package.

This method should be used during development time to verify that the manifest is properly set up, but it doesn't need to be called once the application is deployed to the users' devices.

##### Parameters

context | application context.

##### Throws

IllegalStateException | if any of the conditions above is not met.

#### public static long getRegisterOnServerLifespan (Context context)

Gets how long (in milliseconds) the `isRegistered(Context)` property is valid.

##### Returns

- value set by `setRegisteredOnServer(Context, boolean)` or `DEFAULT_ON_SERVER_LIFESPAN_MS` if not set.

#### public static String getRegistrationId (Context context)

Gets the current registration id for application on GCM service.

If result is empty, the registration has failed.

##### Returns

- registration id, or empty string if the registration is not complete.

#### public static boolean isRegistered (Context context)

Checks whether the application was successfully registered on GCM service.

#### public static boolean isRegisteredOnServer (Context context)

Checks whether the device was successfully registered in the server side, as set by `setRegisteredOnServer(Context, boolean)`.

To avoid the scenario where the device sends the registration to the server but the server loses it, this flag has an expiration date, which is `DEFAULT_ON_SERVER_LIFESPAN_MS` by default (but can be changed by `setRegisterOnServerLifespan(Context, long)`).

#### public static synchronized void onDestroy (Context context)

Clear internal resources.

This method should be called by the main activity's `onDestroy()` method.

#### public static void register (Context context, String... senderIds)

Initiate messaging registration for the current application.

The result will be returned as an `INTENT_FROM_GCM_REGISTRATION_CALLBACK` intent with either a `EXTRA_REGISTRATION_ID` or `EXTRA_ERROR`.

##### Parameters

context | application context.
senderIds | Google Project ID of the accounts authorized to send messages to this application.

##### Throws

IllegalStateException | if device does not have all GCM dependencies installed.

#### public static void setRegisterOnServerLifespan (Context context, long lifespan)

Sets how long (in milliseconds) the `isRegistered(Context)` flag is valid.

#### public static void setRegisteredOnServer (Context context, boolean flag)

Sets whether the device was successfully registered in the server side.

#### public static void unregister (Context context)

Unregister the application.

The result will be returned as an `INTENT_FROM_GCM_REGISTRATION_CALLBACK` intent with an `EXTRA_UNREGISTERED` extra.