Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future. Before you go, please visit our GCM and FCM developer survey to give us feedback!

GCMPubSub Class

GCMPubSub Class Reference

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.

Messages published to the topic will be received as regular GCM messages with "from" set to "/topics/myTopic".

Only topic names that match the pattern "/topics/[a-zA-Z0-9-_.~%]{1,900}" are allowed for subscribing and publishing.

Inherits NSObject.

Instance Method Summary

(void) - subscribeWithToken:topic:options:handler:
 Subscribes an app instance to a topic, enabling it to receive messages sent to that topic. More...
 
(void) - unsubscribeWithToken:topic:options:handler:
 Unsubscribes an app instance from a topic, stopping it from receiving any further messages sent to that topic. More...
 

Class Method Summary

(instancetype) + sharedInstance
 Returns an instance of GCMPubSub. More...
 

Related Function Summary

(Note that these are not member functions.)

typedef void(^ GCMPubSubCompletion )(NSError *error)
 

Method Detail

+ (instancetype) sharedInstance

Returns an instance of GCMPubSub.

Note you need to call GCMService startWithConfig to start using GCM.

Returns
A shared instance of GCMPubSub.
- (void) subscribeWithToken: (NSString *)  token
topic: (NSString *)  topic
options: (NSDictionary *)  options
handler: (GCMPubSubCompletion handler 

Subscribes an app instance to a topic, enabling it to receive messages sent to that topic.

This is an asynchronous call. If subscription fails, GCM invokes the completion callback with the appropriate error.

Call this function from the main thread. GCM is not thread safe.

See also
GCMPubSub unsubscribeWithToken:topic:handler:
Parameters
tokenThe registration token as received from the InstanceID library for a given authorizedEntity and "gcm" scope.
topicThe topic to subscribe to. Should be of the form "/topics/\<topic-name\>".
handlerThe callback handler invoked when the subscribe call ends. In case of success, a nil error is returned. Otherwise, an appropriate error object is returned.
- (void) unsubscribeWithToken: (NSString *)  token
topic: (NSString *)  topic
options: (NSDictionary *)  options
handler: (GCMPubSubCompletion handler 

Unsubscribes an app instance from a topic, stopping it from receiving any further messages sent to that topic.

This is an asynchronous call. If the attempt to unsubscribe fails, we invoke the completion callback passed in with an appropriate error.

Call this function from the main thread.

Parameters
tokenThe token used to subscribe to this topic.
topicThe topic to unsubscribe from.
handlerThe handler that is invoked once the unsubscribe call ends. In case of success, nil error is returned. Otherwise, an appropriate error object is returned.

Related Functions Detail

- (typedef void(^ GCMPubSubCompletion)(NSError *error))
related

The completion handler invoked when the GCM subscribe/unsubscribe returns. If the call fails we return the approprirate error code as documented in GCMService.

Parameters
errorThe error describing subscribe failure else nil.

Send feedback about...

Cloud Messaging
Cloud Messaging