Commit 3049056d authored by Ethan Milano's avatar Ethan Milano Committed by Libin Lu

Adding .remove() method to FCM.on() (#263)

* fixed documentation

* added .unsubscribe() method

* fix

* fix

* remove instead of unsubscribe
parent f16286cb
...@@ -268,7 +268,7 @@ class App extends Component { ...@@ -268,7 +268,7 @@ class App extends Component {
console.log(token) console.log(token)
// store fcm token in your server // store fcm token in your server
}); });
this.notificationUnsubscribe = FCM.on('notification', (notif) => { this.notificationListener = FCM.on('notification', (notif) => {
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
if(notif.local_notification){ if(notif.local_notification){
//this is a local notification //this is a local notification
...@@ -277,16 +277,16 @@ class App extends Component { ...@@ -277,16 +277,16 @@ class App extends Component {
//app is open/resumed because user clicked banner //app is open/resumed because user clicked banner
} }
}); });
this.refreshUnsubscribe = FCM.on('refreshToken', (token) => { this.refreshTokenListener = FCM.on('refreshToken', (token) => {
console.log(token) console.log(token)
// fcm token may not be available on first load, catch it here // fcm token may not be available on first load, catch it here
}); });
} }
componentWillUnmount() { componentWillUnmount() {
// prevent leaking // stop listening for events
this.refreshUnsubscribe(); this.notificationListener.remove();
this.notificationUnsubscribe(); this.refreshTokenListener.remove();
} }
otherMethods(){ otherMethods(){
......
...@@ -73,12 +73,9 @@ FCM.on = (event, callback) => { ...@@ -73,12 +73,9 @@ FCM.on = (event, callback) => {
const nativeEvent = eventsMap[event]; const nativeEvent = eventsMap[event];
if (!nativeEvent) { if (!nativeEvent) {
throw new Error('FCM event must be "refreshToken" or "notification"'); throw new Error('FCM event must be "refreshToken" or "notification"');
}
const listener = DeviceEventEmitter.addListener(nativeEvent, callback);
return function remove() {
listener.remove();
}; };
return DeviceEventEmitter.addListener(nativeEvent, callback);
}; };
FCM.subscribeToTopic = (topic) => { FCM.subscribeToTopic = (topic) => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment