diff --git a/README.md b/README.md index ef963a0269da002ff1397bb6edc11f62d757795c..024e31722e8c5f579b806ae80504b048238d1974 100644 --- a/README.md +++ b/README.md @@ -108,32 +108,30 @@ In [firebase console](https://console.firebase.google.com/), you can get `google ## Usage ```javascript - -import {DeviceEventEmitter} from 'react-native'; -var FCM = require('react-native-fcm'); - -componentWillMount() { -FCM.requestPermissions(); -FCM.getFCMToken().then(data => { - console.log(data.token) -//store fcm token in your server -}); -this.fcmNotifLsnr = DeviceEventEmitter.addListener('FCMNotificationReceived', (notif) => { -//there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload -}); -this.fcmTokenLsnr = DeviceEventEmitter.addListener('FCMTokenRefreshed', (data) => { - console.log(data.token) -//fcm token may not be available on first load, catch it here -}); -} - -componentWillUnmount() { -//prevent leak -this.fcmNotifLsnr.remove(); -this.fcmTokenLsnr.remove(); -} - -} + import FCM from 'react-native-fcm'; + + ... + componentWillMount() { + FCM.requestPermissions(); + FCM.getFCMToken().then(token => { + console.log(token) + // store fcm token in your server + }); + this.notificationUnsubscribe = FCM.on('notification', (notif) => { + // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload + }); + this.refreshUnsubscribe = FCM.on('refresh', (data) => { + console.log(data.token) + // fcm token may not be available on first load, catch it here + }); + } + + componentWillUnmount() { + // prevent leak + this.refreshUnsubscribe(); + this.notificationUnsubscribe(); + } + ... ``` ### Behaviour when sending `notification` and `data` payload through GCM diff --git a/index.js b/index.js index 8088e4ceca22fa72be039df2554689a761f32880..208d253fa8ee469f12b13a713f2d70101019b0f8 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,38 @@ -'use strict'; +import { + NativeModules, + DeviceEventEmitter, +} from 'react-native'; -var React = require('react-native'); -var {NativeModules} = React; +const eventsMap = { + refersh: 'FCMTokenRefreshed', + notification: 'FCMNotificationReceived', +}; -var FIRMessaging = NativeModules.RNFIRMessaging; +const FIRMessaging = NativeModules.RNFIRMessaging; -class FCM { +const FCM = {}; - static getFCMToken() { - return FIRMessaging.getFCMToken(); - } +FCM.getFCMToken = function getFCMToken() { + return FIRMessaging.getFCMToken(); +}; - static requestPermissions() { - return FIRMessaging.requestPermissions(); - } +FCM.requestPermissions = function requestPermissions() { + return FIRMessaging.requestPermissions(); +}; -} +FCM.on = function on(event, callback) { + const nativeEvent = eventsMap[event]; + + const listener = DeviceEventEmitter.addListener(nativeEvent, (params) => { + callback(params); + }); + + return function remove() { + listener.remove(); + }; +}; FCM.initialData = FIRMessaging.initialData; FCM.initialAction = FIRMessaging.initialAction; -module.exports = FCM; \ No newline at end of file +module.exports = FCM;