Skip to content

Instantly share code, notes, and snippets.

@tjkang
Created March 23, 2017 04:11
Show Gist options
  • Save tjkang/0f3396f06f8727bde7a04bb25b41663e to your computer and use it in GitHub Desktop.
Save tjkang/0f3396f06f8727bde7a04bb25b41663e to your computer and use it in GitHub Desktop.
subscribe and unsubscribe to topic for push notification
import React, { Component } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { Actions } from 'react-native-router-flux';
import FCM from 'react-native-fcm';
.....
_onLoginSuccess = () => {
const topic = '/topics/list'
FCM.subscribeToTopic(topic); // subscribe to topic
Actions.main();
}
_onLoginPress = () => {
const { email, password } = this.state;
this.setState({
error: '',
loading: true,
});
firebase.auth().signInWithEmailAndPassword(email, password)
.then(this._onLoginSuccess)
.catch(error => this._onLoginFailure(error));
}
.....
import React, { Component } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { Actions } from 'react-native-router-flux';
import FCM from 'react-native-fcm';
.....
_onLogoutPress = () => {
const topic = '/topics/list'
FCM.unsubscribeFromTopic(userTopic); // unsubscribe to topic
const ref = firebase.database().ref('subjects');
ref.off();
firebase.auth().signOut();
Actions.auth();
};
.....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment