From 36f03299109c9a9c578ea75b0d8cf7c96d456b56 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Thu, 30 Nov 2017 15:47:19 -0500 Subject: [PATCH] update example --- Examples/simple-fcm-client/app/App.js | 5 +- Examples/simple-fcm-client/app/Listeners.js | 85 +++++++++++---------- 2 files changed, 50 insertions(+), 40 deletions(-) diff --git a/Examples/simple-fcm-client/app/App.js b/Examples/simple-fcm-client/app/App.js index 5175a28..944efb6 100644 --- a/Examples/simple-fcm-client/app/App.js +++ b/Examples/simple-fcm-client/app/App.js @@ -16,9 +16,11 @@ import { import FCM from "react-native-fcm"; -require("./Listeners"); +import {registerKilledListener, registerAppListener} from "./Listeners"; import firebaseClient from "./FirebaseClient"; +registerKilledListener(); + export default class App extends Component { constructor(props) { super(props); @@ -30,6 +32,7 @@ export default class App extends Component { } async componentDidMount(){ + registerAppListener(); FCM.getInitialNotification().then(notif => { this.setState({ initNotif: notif diff --git a/Examples/simple-fcm-client/app/Listeners.js b/Examples/simple-fcm-client/app/Listeners.js index de18dc9..0144e1f 100644 --- a/Examples/simple-fcm-client/app/Listeners.js +++ b/Examples/simple-fcm-client/app/Listeners.js @@ -10,46 +10,53 @@ AsyncStorage.getItem('lastNotification').then(data=>{ } }) -FCM.on(FCMEvent.Notification, notif => { - console.log("Notification", notif); - if(notif.local_notification){ - return; - } - if(notif.opened_from_tray){ - return; - } +export function registerKilledListener(){ + // these callback will be triggered even when app is killed + FCM.on(FCMEvent.Notification, notif => { + AsyncStorage.setItem('lastNotification', JSON.stringify(notif)); + }); +} - // write to local storage even when app is killed - 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); + if(notif.local_notification){ + return; + } + if(notif.opened_from_tray){ + return; + } - if(Platform.OS ==='ios'){ - //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. - //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override - //notif._notificationType is available for iOS platfrom - switch(notif._notificationType){ - case NotificationType.Remote: - notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed - break; - case NotificationType.NotificationResponse: - notif.finish(); - break; - case NotificationType.WillPresent: - notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None - break; - } - } -}); + if(Platform.OS ==='ios'){ + //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. + //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override + //notif._notificationType is available for iOS platfrom + switch(notif._notificationType){ + case NotificationType.Remote: + notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed + break; + case NotificationType.NotificationResponse: + notif.finish(); + break; + case NotificationType.WillPresent: + notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None + break; + } + } + }); -FCM.on(FCMEvent.RefreshToken, token => { - console.log("TOKEN (refreshUnsubscribe)", token); - this.props.onChangeToken(token); -}); + FCM.on(FCMEvent.RefreshToken, token => { + console.log("TOKEN (refreshUnsubscribe)", token); + this.props.onChangeToken(token); + }); -FCM.enableDirectChannel(); -FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => { - console.log('direct channel connected' + data); -}); -setTimeout(function() { - FCM.isDirectChannelEstablished().then(d => console.log(d)); -}, 1000); \ No newline at end of file + FCM.enableDirectChannel(); + FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => { + console.log('direct channel connected' + data); + }); + setTimeout(function() { + FCM.isDirectChannelEstablished().then(d => console.log(d)); + }, 1000); +} -- 2.26.2