java.lang.Object | |
↳ | com.google.android.gms.gcm.GcmPubSub |
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 Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns an instance of GCM PubSub.
| |||||||||||
Subscribes an app instance to a topic, enabling it to receive messages sent to that topic.
| |||||||||||
Unsubscribes an app instance from a topic, stopping it from receiving any further messages sent
to that topic.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Returns an instance of GCM PubSub.
Returns | |
---|---|
GcmPubSub |
GcmPubSub instance |
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
Do not call this function on the main thread.
Parameters | |
---|---|
registrationToken |
String : InstanceID token that authorizes topic sender to send messages
to the app instance. |
topic |
String : developer defined topic name. Must match the following regular expression:
"/topics/[a-zA-Z0-9-_.~%]{1,900}". |
extras |
Bundle : (optional) additional information. |
Throws | |
---|---|
IOException |
if the request fails. |
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 |
String : InstanceID token for the same
sender and scope that was previously used for subscribing to the topic. |
topic |
String : from which to stop receiving messages. |
Throws | |
---|---|
IOException |
if the request fails. |