From bf43c42b98dff160d018fe481041797527b5e064 Mon Sep 17 00:00:00 2001 From: renato Date: Wed, 12 Oct 2016 18:24:18 -0300 Subject: [PATCH] Added token to state controll instead of constants --- Examples/simple-fcm-client/app/App.js | 22 ++++++++++++++++--- .../simple-fcm-client/app/FirebaseClient.js | 15 +++++++++---- .../simple-fcm-client/app/PushController.js | 8 +++++++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Examples/simple-fcm-client/app/App.js b/Examples/simple-fcm-client/app/App.js index a358221..a23d555 100644 --- a/Examples/simple-fcm-client/app/App.js +++ b/Examples/simple-fcm-client/app/App.js @@ -16,19 +16,35 @@ import PushController from "./PushController"; import firebaseClient from "./FirebaseClient"; export default class App extends Component { + constructor(props) { + super(props); + + this.state = { + token: "" + } + } + render() { + let { token } = this.state; + return ( - + this.setState({token: token || ""})} + /> Welcome to Simple Fcm Client! - firebaseClient.sendNotification()} style={styles.button}> + + Token: {this.state.token} + + + firebaseClient.sendNotification(token)} style={styles.button}> Send Notification - firebaseClient.sendData()} style={styles.button}> + firebaseClient.sendData(token)} style={styles.button}> Send Data diff --git a/Examples/simple-fcm-client/app/FirebaseClient.js b/Examples/simple-fcm-client/app/FirebaseClient.js index f6bb1bd..1f13297 100644 --- a/Examples/simple-fcm-client/app/FirebaseClient.js +++ b/Examples/simple-fcm-client/app/FirebaseClient.js @@ -4,9 +4,14 @@ const API_URL = "https://fcm.googleapis.com/fcm/send"; class FirebaseClient { - sendNotification() { + constructor() { + this.sendData = this.sendData.bind(this); + this.sendNotification = this.sendNotification.bind(this); + } + + sendNotification(token) { let body = { - "to": FirebaseConstants.TO, + "to": token, "notification":{ "icon": "appLogo", "title": "Notification Title", @@ -14,15 +19,16 @@ class FirebaseClient { "sound": "default", "click_action": "fcm.ACTION.HELLO" }, + "content_available": true, "priority": 10 } this._send(JSON.stringify(body), "notification"); } - sendData() { + sendData(token) { let body = { - "to": FirebaseConstants.TO, + "to": token, "data":{ "icon": "appLogo", "title": "Notification Title", @@ -30,6 +36,7 @@ class FirebaseClient { "sound": "default", "click_action": "fcm.ACTION.HELLO" }, + "content_available": true, "priority": 10 } diff --git a/Examples/simple-fcm-client/app/PushController.js b/Examples/simple-fcm-client/app/PushController.js index 5fa9c53..c6ca152 100644 --- a/Examples/simple-fcm-client/app/PushController.js +++ b/Examples/simple-fcm-client/app/PushController.js @@ -2,12 +2,19 @@ import React, { Component } from "react"; import FCM from "react-native-fcm"; +import firebaseClient from "./FirebaseClient"; + export default class PushController extends Component { + constructor(props) { + super(props); + } + componentDidMount() { FCM.requestPermissions(); FCM.getFCMToken().then(token => { console.log("TOKEN (getFCMToken)", token); + this.props.onChangeToken(token); }); FCM.getInitialNotification().then(notif => { @@ -20,6 +27,7 @@ export default class PushController extends Component { this.refreshUnsubscribe = FCM.on("refreshToken", token => { console.log("TOKEN (refreshUnsubscribe)", token); + this.props.onChangeToken(token); }); } -- 2.26.2