Commit 36f03299 authored by Libin Lu's avatar Libin Lu

update example

parent 20eb97ec
...@@ -16,9 +16,11 @@ import { ...@@ -16,9 +16,11 @@ import {
import FCM from "react-native-fcm"; import FCM from "react-native-fcm";
require("./Listeners"); import {registerKilledListener, registerAppListener} from "./Listeners";
import firebaseClient from "./FirebaseClient"; import firebaseClient from "./FirebaseClient";
registerKilledListener();
export default class App extends Component { export default class App extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
...@@ -30,6 +32,7 @@ export default class App extends Component { ...@@ -30,6 +32,7 @@ export default class App extends Component {
} }
async componentDidMount(){ async componentDidMount(){
registerAppListener();
FCM.getInitialNotification().then(notif => { FCM.getInitialNotification().then(notif => {
this.setState({ this.setState({
initNotif: notif initNotif: notif
......
...@@ -10,7 +10,16 @@ AsyncStorage.getItem('lastNotification').then(data=>{ ...@@ -10,7 +10,16 @@ AsyncStorage.getItem('lastNotification').then(data=>{
} }
}) })
FCM.on(FCMEvent.Notification, notif => { export function registerKilledListener(){
// these callback will be triggered even when app is killed
FCM.on(FCMEvent.Notification, notif => {
AsyncStorage.setItem('lastNotification', JSON.stringify(notif));
});
}
// these callback will be triggered only when app is foreground or background
export function registerAppListener(){
FCM.on(FCMEvent.Notification, notif => {
console.log("Notification", notif); console.log("Notification", notif);
if(notif.local_notification){ if(notif.local_notification){
return; return;
...@@ -19,9 +28,6 @@ FCM.on(FCMEvent.Notification, notif => { ...@@ -19,9 +28,6 @@ FCM.on(FCMEvent.Notification, notif => {
return; return;
} }
// write to local storage even when app is killed
AsyncStorage.setItem('lastNotification', JSON.stringify(notif));
if(Platform.OS ==='ios'){ if(Platform.OS ==='ios'){
//optional //optional
//iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link. //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
...@@ -39,17 +45,18 @@ FCM.on(FCMEvent.Notification, notif => { ...@@ -39,17 +45,18 @@ FCM.on(FCMEvent.Notification, notif => {
break; break;
} }
} }
}); });
FCM.on(FCMEvent.RefreshToken, token => { FCM.on(FCMEvent.RefreshToken, token => {
console.log("TOKEN (refreshUnsubscribe)", token); console.log("TOKEN (refreshUnsubscribe)", token);
this.props.onChangeToken(token); this.props.onChangeToken(token);
}); });
FCM.enableDirectChannel(); FCM.enableDirectChannel();
FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => { FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => {
console.log('direct channel connected' + data); console.log('direct channel connected' + data);
}); });
setTimeout(function() { setTimeout(function() {
FCM.isDirectChannelEstablished().then(d => console.log(d)); FCM.isDirectChannelEstablished().then(d => console.log(d));
}, 1000); }, 1000);
\ 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