Files
react-native-firebase/docs/api/cloud-messaging.md
2017-03-21 11:25:07 +00:00

3.5 KiB

Cloud Messaging

Firebase Cloud Messaging (FCM) allows you to send push messages at no cost to both Android & iOS platforms. Assuming the installation instructions have been followed, FCM is ready to go.

As the Firebase Web SDK has limited messaging functionality, the following methods within react-native-firebase have been created to handle FCM in the React Native environment.

API

subscribeToTopic(topic: string)

Subscribes the device to a topic.

firebase.messaging().subscribeToTopic('foobar');

unsubscribeFromTopic(topic: string)

Unsubscribes the device from a topic.

firebase.messaging().unsubscribeFromTopic('foobar');

getInitialNotification(): Promise<Object>

When the application has been opened from a notification getInitialNotification is called and the notification payload is returned. Use onMessage for notifications when the app is running.

firebase.messaging().getInitialNotification()
  .then((notification) => {
    console.log('Notification which opened the app: ', notification);
  });

### getToken(): Promise

Returns the devices FCM token. This token can be used in the Firebase console to send messages to directly.

firebase.messaging().getToken()
  .then((token) => {
    console.log('Device FCM Token: ', token);
  });

onTokenRefresh(listener: Function)

On the event a devices FCM token is refreshed by Google, the new token is returned in a callback listener.

firebase.messaging().onTokenRefresh((token) => {
  console.log('Refreshed FCM token: ', token);
});

onMessage(listener: Function