public class

FirebaseMessaging

extends Object
java.lang.Object
   ↳ com.google.firebase.messaging.FirebaseMessaging

Class Overview

Top level Firebase Cloud Messaging singleton that provides methods for subscribing to topics and sending upstream messages.

In order to receive Firebase messages, declare an implementation of FirebaseMessagingService in the app manifest. To process messages, override base class methods to handle any events required by the application.

Client apps can send upstream messages back to the app server using the XMPP-based Cloud Connection Server. For example:

 FirebaseMessaging.getInstance().send(
     new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com")
     .setMessageId(id)
     .addData("key", "value")
     .build());

Summary

Constants
String INSTANCE_ID_SCOPE Specifies scope used in obtaining a registration token when calling getToken()
Public Methods
static FirebaseMessaging getInstance()
boolean isAutoInitEnabled()
Determine whether FCM auto-initialization is enabled or disabled.
void send(RemoteMessage message)
Sends message upstream to your app server.
void setAutoInitEnabled(boolean enable)
Enable or disable auto-initialization of Firebase Cloud Messaging.
void subscribeToTopic(String topic)
Subscribes to topic in the background.
void unsubscribeFromTopic(String topic)
Unsubscribes from topic in the background.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String INSTANCE_ID_SCOPE

Specifies scope used in obtaining a registration token when calling getToken()

Constant Value: "FCM"

Public Methods

public static FirebaseMessaging getInstance ()

public boolean isAutoInitEnabled ()

Determine whether FCM auto-initialization is enabled or disabled.

Returns
boolean true if auto-init is enabled and false if auto-init is disabled

public void send (RemoteMessage message)

Sends message upstream to your app server.

When there is an active connection the message will be sent immediately, otherwise the message will be queued up to the time to live (TTL) set in the message.

Parameters
message RemoteMessage

public void setAutoInitEnabled (boolean enable)

Enable or disable auto-initialization of Firebase Cloud Messaging.

When enabled, FCM generates a registration token on app startup if there is no valid one and generates a new token when it is deleted (which prevents deleteInstanceId() from stopping the periodic sending of data). This setting is persisted across app restarts and overrides the setting specified in your manifest.

By default, FCM auto-initialization is enabled. If you need to change the default, (for example, because you want to prompt the user before FCM generates/refreshes a registration token on app startup), add to your application’s manifest:

<meta-data android:name="firebase_messaging_auto_init_enabled" android:value="false" />
 

Parameters
enable boolean: Whether FCM should auto-initialize.

public void subscribeToTopic (String topic)

Subscribes to topic in the background.

This uses a Firebase Instance ID token to identify the app instance and periodically sends data to the Firebase backend. To stop this, see deleteInstanceId().

Parameters
topic String: The name of the topic to subscribe. Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}".

public void unsubscribeFromTopic (String topic)

Unsubscribes from topic in the background.

This does not stop FirebaseInstanceId's periodic sending of data started by subscribeToTopic(String). To stop this, see deleteInstanceId().

Parameters
topic String: The name of the topic to unsubscribe from. Must match the following regular expression: "[a-zA-Z0-9-_.~%]{1,900}".