GcmPubSub provides a publish-subscribe model for sending GCM topic messages.
An app can subscribe to different topics defined by the developer. The app server can then send messages to the subscribed devices without having to maintain topic-subscribers mapping. Topics do not need to be explicitly created before subscribing or publishing—they are automatically created when publishing or subscribing.String topic = "/topics/myTopic"; String registrationToken = InstanceID.getInstance(context) .getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); GcmPubSub.getInstance(context).subscribe(registrationToken, topic, null); // Messages published to the topic will be received as regular GCM messages // with 'from' set to "/topics/myTopic"To publish to a topic, see GCM server documentation.
Public Method Summary
synchronized static GcmPubSub | |
void | |
void |
unsubscribe(String registrationToken, String topic)
Unsubscribes an app instance from a topic, stopping it from receiving
any further messages sent to that topic.
|
Inherited Method Summary
Public Methods
public static synchronized GcmPubSub getInstance (Context context)
Returns an instance of GCM PubSub.
Returns
- GcmPubSub instance
public void subscribe (String registrationToken, String topic, Bundle extras)
Subscribes an app instance to a topic, enabling it to receive messages sent to that topic.
The topic sender must be authorized to send messages to the
app instance. To authorize it, call getToken(String, String)
with the sender ID and INSTANCE_ID_SCOPE
Parameters
registrationToken | InstanceID token that authorizes topic
sender to send messages to the app instance. |
---|---|
topic | developer defined topic name. Must match the following regular expression: "/topics/[a-zA-Z0-9-_.~%]{1,900}". |
extras | (optional) additional information. |
Throws
IOException | if the request fails. |
---|
public void unsubscribe (String registrationToken, String topic)
Unsubscribes an app instance from a topic, stopping it from receiving any further messages sent to that topic.
Do not call this function on the main thread.Parameters
registrationToken | InstanceID token
for the same sender and scope that was previously
used for subscribing to the topic. |
---|---|
topic | from which to stop receiving messages. |
Throws
IOException | if the request fails. |
---|