public final class GCMRegistrar
extends java.lang.Object
Note: this class uses a private SharedPreferences
object to keep track of the registration token.
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_ON_SERVER_LIFESPAN_MS
Default lifespan (7 days) of the
isRegisteredOnServer(Context)
flag until it is considered expired. |
Modifier and Type | Method and Description |
---|---|
static void |
checkDevice(android.content.Context context)
Checks if the device has the proper dependencies installed.
|
static void |
checkManifest(android.content.Context context)
Checks that the application manifest is properly configured.
|
static long |
getRegisterOnServerLifespan(android.content.Context context)
Gets how long (in milliseconds) the
isRegistered(Context)
property is valid. |
static java.lang.String |
getRegistrationId(android.content.Context context)
Gets the current registration id for application on GCM service.
|
static boolean |
isRegistered(android.content.Context context)
Checks whether the application was successfully registered on GCM
service.
|
static boolean |
isRegisteredOnServer(android.content.Context context)
Checks whether the device was successfully registered in the server side,
as set by
setRegisteredOnServer(Context, boolean) . |
static void |
onDestroy(android.content.Context context)
Clear internal resources.
|
static void |
register(android.content.Context context,
java.lang.String... senderIds)
Initiate messaging registration for the current application.
|
static void |
setRegisteredOnServer(android.content.Context context,
boolean flag)
Sets whether the device was successfully registered in the server side.
|
static void |
setRegisterOnServerLifespan(android.content.Context context,
long lifespan)
Sets how long (in milliseconds) the
isRegistered(Context)
flag is valid. |
static void |
unregister(android.content.Context context)
Unregister the application.
|
public static final long DEFAULT_ON_SERVER_LIFESPAN_MS
isRegisteredOnServer(Context)
flag until it is considered expired.public static void checkDevice(android.content.Context context)
This method should be called when the application starts to verify that the device supports GCM.
context
- application context.java.lang.UnsupportedOperationException
- if the device does not support GCM.public static void checkManifest(android.content.Context context)
A proper configuration means:
PACKAGE_NAME.permission.C2D_MESSAGE
.
BroadcastReceiver
with category
PACKAGE_NAME
.
BroadcastReceiver
(s) uses the
permission.
BroadcastReceiver
(s) handles the 2 GCM intents
( and
).
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.
context
- application context.java.lang.IllegalStateException
- if any of the conditions above is not met.public static void register(android.content.Context context, java.lang.String... senderIds)
The result will be returned as an
GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK
intent with
either a GCMConstants.EXTRA_REGISTRATION_ID
or
GCMConstants.EXTRA_ERROR
.
context
- application context.senderIds
- Google Project ID of the accounts authorized to send
messages to this application.java.lang.IllegalStateException
- if device does not have all GCM
dependencies installed.public static void unregister(android.content.Context context)
The result will be returned as an
GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK
intent with an
GCMConstants.EXTRA_UNREGISTERED
extra.
public static void onDestroy(android.content.Context context)
This method should be called by the main activity's onDestroy()
method.
public static java.lang.String getRegistrationId(android.content.Context context)
If result is empty, the registration has failed.
public static boolean isRegistered(android.content.Context context)
public static void setRegisteredOnServer(android.content.Context context, boolean flag)
public static boolean isRegisteredOnServer(android.content.Context context)
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 long getRegisterOnServerLifespan(android.content.Context context)
isRegistered(Context)
property is valid.setRegisteredOnServer(Context, boolean)
or
DEFAULT_ON_SERVER_LIFESPAN_MS
if not set.public static void setRegisterOnServerLifespan(android.content.Context context, long lifespan)
isRegistered(Context)
flag is valid.