Commit 421f1aa0 authored by Libin Lu's avatar Libin Lu Committed by GitHub

Update README.md

parent 8a3d1240
...@@ -284,16 +284,8 @@ NOTE: `com.evollu.react.fcm.FIRLocalMessagingPublisher` is required for presenti ...@@ -284,16 +284,8 @@ NOTE: `com.evollu.react.fcm.FIRLocalMessagingPublisher` is required for presenti
import {Platform} from 'react-native'; import {Platform} from 'react-native';
import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm'; import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';
class App extends Component { // this shall be called regardless of app state: running, background or not running. Won't be called when app is killed by user in iOS
componentDidMount() { FCM.on(FCMEvent.Notification, async (notif) => {
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
// NOTE: if you want to handle notifications when app is closed, put another handler outside of component lifecycle because components won't be initialized.
this.notificationListener = FCM.on(FCMEvent.Notification, async (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
...@@ -320,17 +312,28 @@ class App extends Component { ...@@ -320,17 +312,28 @@ class App extends Component {
break; break;
} }
} }
}); });
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => { FCM.on(FCMEvent.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
});
class App extends Component {
componentDidMount() {
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
// do some component related stuff
}); });
} }
componentWillUnmount() { componentWillUnmount() {
// stop listening for events // stop listening for events
this.notificationListener.remove(); this.notificationListener.remove();
this.refreshTokenListener.remove();
} }
otherMethods(){ otherMethods(){
......
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