From 3049056dc7679ba81199e418eda252c164f2ecfa Mon Sep 17 00:00:00 2001 From: Ethan Milano Date: Fri, 6 Jan 2017 22:09:26 +0100 Subject: [PATCH] Adding .remove() method to FCM.on() (#263) * fixed documentation * added .unsubscribe() method * fix * fix * remove instead of unsubscribe --- README.md | 20 ++++++++++---------- index.js | 7 ++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0c02671..cb3bee4 100644 --- a/README.md +++ b/README.md @@ -218,12 +218,12 @@ Edit `AppDelegate.m`: + completionHandler(UIBackgroundFetchResultNoData); + } ``` - + ### Xcode post installation steps - Select your project **Capabilities** and enable **Keychan Sharing** and *Background Modes* > **Remote notifications**. - -- In Xcode menu bar, select *Product* > *Scheme* > **Manage schemes**. Select your project name Scheme then click on the minus sign **―** in the bottom left corner, then click on the plus sign **+** and rebuild your project scheme. - + +- In Xcode menu bar, select *Product* > *Scheme* > **Manage schemes**. Select your project name Scheme then click on the minus sign **―** in the bottom left corner, then click on the plus sign **+** and rebuild your project scheme. + ### FCM config file In [firebase console](https://console.firebase.google.com/), you can get `google-services.json` file and place it in `android/app` directory and get `GoogleService-Info.plist` file and place it in `/ios/your-project-name` directory (next to your `Info.plist`) @@ -268,7 +268,7 @@ class App extends Component { console.log(token) // 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 if(notif.local_notification){ //this is a local notification @@ -277,16 +277,16 @@ class App extends Component { //app is open/resumed because user clicked banner } }); - this.refreshUnsubscribe = FCM.on('refreshToken', (token) => { + this.refreshTokenListener = FCM.on('refreshToken', (token) => { console.log(token) // fcm token may not be available on first load, catch it here }); } componentWillUnmount() { - // prevent leaking - this.refreshUnsubscribe(); - this.notificationUnsubscribe(); + // stop listening for events + this.notificationListener.remove(); + this.refreshTokenListener.remove(); } otherMethods(){ @@ -480,7 +480,7 @@ search for `compile "com.google.android.gms` in android and see who specifies sp Check open from tray flag in notification. It will be either 0 or 1 for iOS and undefined or 1 for android. I decide for iOS based on [this](http://stackoverflow.com/questions/20569201/remote-notification-method-called-twice), and for android I set it if notification is triggered by intent change. #### Android notification doesn't vibrate/show head-up display etc -All available features are [here](https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support). FCM may add more support in the future but there is no timeline. +All available features are [here](https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support). FCM may add more support in the future but there is no timeline. In the mean time, you can pass "custom_notification" in a data message. This repo will show a local notification for you so you can set priority etc #### How do I do xxx with FCM? diff --git a/index.js b/index.js index 6a2711c..f3a2045 100644 --- a/index.js +++ b/index.js @@ -73,12 +73,9 @@ FCM.on = (event, callback) => { const nativeEvent = eventsMap[event]; if (!nativeEvent) { 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) => { -- 2.26.2