diff --git a/Examples/simple-fcm-client/app/App.js b/Examples/simple-fcm-client/app/App.js index cff58090e75ce066ad191a4d8e3cfdbdbc07dd57..5175a28ce26d5f7062b2da0a5bbc49344e0d961b 100644 --- a/Examples/simple-fcm-client/app/App.js +++ b/Examples/simple-fcm-client/app/App.js @@ -16,7 +16,7 @@ import { import FCM from "react-native-fcm"; -import PushController from "./PushController"; +require("./Listeners"); import firebaseClient from "./FirebaseClient"; export default class App extends Component { @@ -29,12 +29,29 @@ export default class App extends Component { } } - componentDidMount(){ + async componentDidMount(){ FCM.getInitialNotification().then(notif => { this.setState({ initNotif: notif }) }); + + try{ + let result = await FCM.requestPermissions({badge: false, sound: true, alert: true}); + } catch(e){ + console.error(e); + } + + FCM.getFCMToken().then(token => { + console.log("TOKEN (getFCMToken)", token); + this.setState({token: token || ""}) + }); + + if(Platform.OS === 'ios'){ + FCM.getAPNSToken().then(token => { + console.log("APNS TOKEN (getFCMToken)", token); + }); + } } showLocalNotification() { @@ -134,9 +151,6 @@ export default class App extends Component { return ( - this.setState({token: token || ""})} - /> Welcome to Simple Fcm Client! diff --git a/Examples/simple-fcm-client/app/Listeners.js b/Examples/simple-fcm-client/app/Listeners.js new file mode 100644 index 0000000000000000000000000000000000000000..de18dc9e14378ecdab68efdea0bd9d8e433b8138 --- /dev/null +++ b/Examples/simple-fcm-client/app/Listeners.js @@ -0,0 +1,55 @@ +import { Platform, AsyncStorage } from 'react-native'; + +import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from "react-native-fcm"; + +AsyncStorage.getItem('lastNotification').then(data=>{ + if(data){ + // if notification arrives when app is killed, it should still be logged here + console.log('last notification', JSON.parse(data)); + AsyncStorage.removeItem('lastNotification'); + } +}) + +FCM.on(FCMEvent.Notification, notif => { + console.log("Notification", notif); + if(notif.local_notification){ + return; + } + if(notif.opened_from_tray){ + return; + } + + // write to local storage even when app is killed + AsyncStorage.setItem('lastNotification', JSON.stringify(notif)); + + 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.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 diff --git a/Examples/simple-fcm-client/app/PushController.js b/Examples/simple-fcm-client/app/PushController.js deleted file mode 100644 index a53fd541dac76381f26c7c6a143fce873ede0c6f..0000000000000000000000000000000000000000 --- a/Examples/simple-fcm-client/app/PushController.js +++ /dev/null @@ -1,90 +0,0 @@ -import React, { Component } from "react"; - -import { Platform } from 'react-native'; - -import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from "react-native-fcm"; - -import firebaseClient from "./FirebaseClient"; - -export default class PushController extends Component { - constructor(props) { - super(props); - } - - async componentDidMount() { - - try{ - let result = await FCM.requestPermissions({badge: false, sound: true, alert: true}); - } catch(e){ - console.error(e); - } - - FCM.getFCMToken().then(token => { - console.log("TOKEN (getFCMToken)", token); - this.props.onChangeToken(token); - }); - - if(Platform.OS === 'ios'){ - FCM.getAPNSToken().then(token => { - console.log("APNS TOKEN (getFCMToken)", token); - }); - } - - FCM.getInitialNotification().then(notif => { - console.log("INITIAL NOTIFICATION", notif) - }); - - this.notificationListener = 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; - } - } - - this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => { - console.log("TOKEN (refreshUnsubscribe)", token); - this.props.onChangeToken(token); - }); - - // direct channel related methods are ios only - // directly channel is truned off in iOS by default, this method enables it - FCM.enableDirectChannel(); - this.channelConnectionListener = FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => { - console.log('direct channel connected' + data); - }); - setTimeout(function() { - FCM.isDirectChannelEstablished().then(d => console.log(d)); - }, 1000); - }) - } - - componentWillUnmount() { - this.notificationListener.remove(); - this.refreshTokenListener.remove(); - } - - - render() { - return null; - } -}