Commit 48770b8f authored by Libin Lu's avatar Libin Lu

Merge pull request #11 from gorangajic/master

make lib more user friendly
parents b53e226f 7a9934ab
......@@ -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
......
'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;
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