Instance ID provides a unique identifier for each app instance and a mechanism to authenticate and authorize actions (for example, sending a GCM message).
Instance ID is stable but may become invalid, if:- App deletes Instance ID
- Device is factory reset
- User uninstalls the app
- User clears app data
getId()
to request a new Instance ID.
To prove ownership of Instance ID and to allow servers to access data or
services associated with the app, call getToken(String, String)
.
Constant Summary
String | ERROR_BACKOFF | Error returned when failed requests are retried too often. |
String | ERROR_MAIN_THREAD | Blocking methods must not be called on the main thread. |
String | ERROR_MISSING_INSTANCEID_SERVICE | Tokens can't be generated. |
String | ERROR_SERVICE_NOT_AVAILABLE | The device cannot read the response, or there was a server error. |
String | ERROR_TIMEOUT | Timeout waiting for a response. |
Public Method Summary
void |
deleteInstanceID()
Resets Instance ID and revokes all tokens.
|
void |
deleteToken(String authorizedEntity, String scope)
Revokes access to a scope (action) for an entity previously
authorized by
getToken(String, String) . |
long |
getCreationTime()
Returns time when instance ID was created.
|
String |
getId()
Returns a stable identifier that uniquely identifies the app instance.
|
static InstanceID | |
String | |
String |
Inherited Method Summary
Constants
public static final String ERROR_BACKOFF
Error returned when failed requests are retried too often. Use exponential backoff when retrying requests
public static final String ERROR_MAIN_THREAD
Blocking methods must not be called on the main thread.
public static final String ERROR_MISSING_INSTANCEID_SERVICE
Tokens can't be generated. Only devices with Google Play are supported.
public static final String ERROR_SERVICE_NOT_AVAILABLE
The device cannot read the response, or there was a server error. Application should retry the request later using exponential backoff and retry (on each subsequent failure increase delay before retrying).
Public Methods
public void deleteToken (String authorizedEntity, String scope)
Revokes access to a scope (action) for an entity previously
authorized by getToken(String, String)
.
Parameters
authorizedEntity | Entity that must no longer have access. |
---|---|
scope | Action that entity is no longer authorized to perform. |
Throws
IOException | if the request fails. |
---|
public long getCreationTime ()
Returns time when instance ID was created.
Returns
- Time when instance ID was created (milliseconds since Epoch).
public String getId ()
Returns a stable identifier that uniquely identifies the app instance.
Returns
- The identifier for the application instance.
public static InstanceID getInstance (Context context)
Returns an instance of this class.
Returns
- InstanceID instance.
public String getToken (String authorizedEntity, String scope)
Returns a token that authorizes an Entity (example: cloud service) to perform an action on behalf of the application identified by Instance ID.
This is similar to an OAuth2 token except, it applies to the application instance instead of a user. Do not call this function on the main thread.Parameters
authorizedEntity | Entity authorized by the token. |
---|---|
scope | Action authorized for authorizedEntity. |
Returns
- a token that can identify and authorize the instance of the application on the device.
Throws
IOException | if the request fails. |
---|
public String getToken (String authorizedEntity, String scope, Bundle extras)
Returns a token that authorizes an Entity (example: cloud service) to perform an action on behalf of the application identified by Instance ID.
This is similar to an OAuth2 token except, it applies to the application instance instead of a user. Do not call this function on the main thread.Parameters
authorizedEntity | Entity authorized by the token. |
---|---|
scope | Action authorized for authorizedEntity. |
extras | additional parameters specific to each token scope. Bundle keys starting with 'GCM.' and 'GOOGLE.' are reserved. |
Returns
- a token that can identify and authorize the instance of the application on the device.
Throws
IOException | if the request fails. |
---|