Commit 7a9934ab authored by Goran Gajic's avatar Goran Gajic

make lib more user friendly

parent 5ee12933
...@@ -113,32 +113,30 @@ In [firebase console](https://console.firebase.google.com/), you can get `google ...@@ -113,32 +113,30 @@ In [firebase console](https://console.firebase.google.com/), you can get `google
## Usage ## Usage
```javascript ```javascript
import FCM from 'react-native-fcm';
import {DeviceEventEmitter} from 'react-native';
var FCM = require('react-native-fcm'); ...
componentWillMount() {
componentWillMount() { FCM.requestPermissions();
FCM.requestPermissions(); FCM.getFCMToken().then(token => {
FCM.getFCMToken().then(data => { console.log(token)
console.log(data.token) // store fcm token in your server
//store fcm token in your server });
}); this.notificationUnsubscribe = FCM.on('notification', (notif) => {
this.fcmNotifLsnr = DeviceEventEmitter.addListener('FCMNotificationReceived', (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 });
}); this.refreshUnsubscribe = FCM.on('refresh', (data) => {
this.fcmTokenLsnr = DeviceEventEmitter.addListener('FCMTokenRefreshed', (data) => { console.log(data.token)
console.log(data.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 leak
//prevent leak this.refreshUnsubscribe();
this.fcmNotifLsnr.remove(); this.notificationUnsubscribe();
this.fcmTokenLsnr.remove(); }
} ...
}
``` ```
### Behaviour when sending `notification` and `data` payload through GCM ### Behaviour when sending `notification` and `data` payload through GCM
......
'use strict'; import {
NativeModules,
DeviceEventEmitter,
} from 'react-native';
var React = require('react-native'); const eventsMap = {
var {NativeModules} = React; refersh: 'FCMTokenRefreshed',
notification: 'FCMNotificationReceived',
};
var FIRMessaging = NativeModules.RNFIRMessaging; const FIRMessaging = NativeModules.RNFIRMessaging;
class FCM { const FCM = {};
static getFCMToken() { FCM.getFCMToken = function getFCMToken() {
return FIRMessaging.getFCMToken(); return FIRMessaging.getFCMToken();
} };
static requestPermissions() { FCM.requestPermissions = function requestPermissions() {
return FIRMessaging.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.initialData = FIRMessaging.initialData;
FCM.initialAction = FIRMessaging.initialAction; FCM.initialAction = FIRMessaging.initialAction;
module.exports = FCM; module.exports = FCM;
\ No newline at end of file
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