public class

InstanceID

extends Object
java.lang.Object
   ↳ com.google.android.gms.iid.InstanceID

Class Overview

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
If Instance ID has become invalid, the app can call 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).

Summary

Constants
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 Methods
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 getInstance(Context context)
Returns an instance of this class.
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.
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.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ERROR_BACKOFF

Error returned when failed requests are retried too often. Use exponential backoff when retrying requests

Constant Value: "RETRY_LATER"

public static final String ERROR_MAIN_THREAD

Blocking methods must not be called on the main thread.

Constant Value: "MAIN_THREAD"

public static final String ERROR_MISSING_INSTANCEID_SERVICE

Tokens can't be generated. Only devices with Google Play are supported.

Constant Value: "MISSING_INSTANCEID_SERVICE"

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).

Constant Value: "SERVICE_NOT_AVAILABLE"

public static final String ERROR_TIMEOUT

Timeout waiting for a response.

Constant Value: "TIMEOUT"

Public Methods

public void deleteInstanceID ()

Resets Instance ID and revokes all tokens.

Throws
IOException

public void deleteToken (String authorizedEntity, String scope)

Revokes access to a scope (action) for an entity previously authorized by getToken(String, String).

Do not call this function on the main thread.

Parameters
authorizedEntity String: Entity that must no longer have access.
scope String: 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
long Time when instance ID was created (milliseconds since Epoch).

public String getId ()

Returns a stable identifier that uniquely identifies the app instance.

Returns
String The identifier for the application instance.

public static InstanceID getInstance (Context context)

Returns an instance of this class.

Parameters
context Context
Returns
InstanceID 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 String: Entity authorized by the token.
scope String: Action authorized for authorizedEntity.
Returns
String 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 String: Entity authorized by the token.
scope String: Action authorized for authorizedEntity.
extras Bundle: additional parameters specific to each token scope. Bundle keys starting with 'GCM.' and 'GOOGLE.' are reserved.
Returns
String a token that can identify and authorize the instance of the application on the device.
Throws
IOException if the request fails.