Summary: Methods | Inherited Methods | [Expand All]
public class

# GcmPubSub

extends Object

java.lang.Object
↳ | com.google.android.gms.gcm.GcmPubSub

## Class Overview

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.

## Summary

Public Methods
static GcmPubSub | getInstance(Context context) Returns an instance of GCM PubSub.
void | subscribe(String registrationToken, String topic, Bundle extras) Subscribes an app instance to a topic, enabling it to receive messages sent to that topic.
void | unsubscribe(String registrationToken, String topic) Unsubscribes an app instance from a topic, stopping it from receiving any further messages sent to that topic.

[Expand] Inherited Methods
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
final void | wait()

## Public Methods

#### public static 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`

Do not call this function on the main thread.

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