Commit bf43c42b authored by renato's avatar renato

Added token to state controll instead of constants

parent 58694566
...@@ -16,19 +16,35 @@ import PushController from "./PushController"; ...@@ -16,19 +16,35 @@ import PushController from "./PushController";
import firebaseClient from "./FirebaseClient"; import firebaseClient from "./FirebaseClient";
export default class App extends Component { export default class App extends Component {
constructor(props) {
super(props);
this.state = {
token: ""
}
}
render() { render() {
let { token } = this.state;
return ( return (
<View style={styles.container}> <View style={styles.container}>
<PushController /> <PushController
onChangeToken={token => this.setState({token: token || ""})}
/>
<Text style={styles.welcome}> <Text style={styles.welcome}>
Welcome to Simple Fcm Client! Welcome to Simple Fcm Client!
</Text> </Text>
<TouchableOpacity onPress={() => firebaseClient.sendNotification()} style={styles.button}> <Text style={styles.instructions}>
Token: {this.state.token}
</Text>
<TouchableOpacity onPress={() => firebaseClient.sendNotification(token)} style={styles.button}>
<Text style={styles.buttonText}>Send Notification</Text> <Text style={styles.buttonText}>Send Notification</Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity onPress={() => firebaseClient.sendData()} style={styles.button}> <TouchableOpacity onPress={() => firebaseClient.sendData(token)} style={styles.button}>
<Text style={styles.buttonText}>Send Data</Text> <Text style={styles.buttonText}>Send Data</Text>
</TouchableOpacity> </TouchableOpacity>
</View> </View>
......
...@@ -4,9 +4,14 @@ const API_URL = "https://fcm.googleapis.com/fcm/send"; ...@@ -4,9 +4,14 @@ const API_URL = "https://fcm.googleapis.com/fcm/send";
class FirebaseClient { class FirebaseClient {
sendNotification() { constructor() {
this.sendData = this.sendData.bind(this);
this.sendNotification = this.sendNotification.bind(this);
}
sendNotification(token) {
let body = { let body = {
"to": FirebaseConstants.TO, "to": token,
"notification":{ "notification":{
"icon": "appLogo", "icon": "appLogo",
"title": "Notification Title", "title": "Notification Title",
...@@ -14,15 +19,16 @@ class FirebaseClient { ...@@ -14,15 +19,16 @@ class FirebaseClient {
"sound": "default", "sound": "default",
"click_action": "fcm.ACTION.HELLO" "click_action": "fcm.ACTION.HELLO"
}, },
"content_available": true,
"priority": 10 "priority": 10
} }
this._send(JSON.stringify(body), "notification"); this._send(JSON.stringify(body), "notification");
} }
sendData() { sendData(token) {
let body = { let body = {
"to": FirebaseConstants.TO, "to": token,
"data":{ "data":{
"icon": "appLogo", "icon": "appLogo",
"title": "Notification Title", "title": "Notification Title",
...@@ -30,6 +36,7 @@ class FirebaseClient { ...@@ -30,6 +36,7 @@ class FirebaseClient {
"sound": "default", "sound": "default",
"click_action": "fcm.ACTION.HELLO" "click_action": "fcm.ACTION.HELLO"
}, },
"content_available": true,
"priority": 10 "priority": 10
} }
......
...@@ -2,12 +2,19 @@ import React, { Component } from "react"; ...@@ -2,12 +2,19 @@ import React, { Component } from "react";
import FCM from "react-native-fcm"; import FCM from "react-native-fcm";
import firebaseClient from "./FirebaseClient";
export default class PushController extends Component { export default class PushController extends Component {
constructor(props) {
super(props);
}
componentDidMount() { componentDidMount() {
FCM.requestPermissions(); FCM.requestPermissions();
FCM.getFCMToken().then(token => { FCM.getFCMToken().then(token => {
console.log("TOKEN (getFCMToken)", token); console.log("TOKEN (getFCMToken)", token);
this.props.onChangeToken(token);
}); });
FCM.getInitialNotification().then(notif => { FCM.getInitialNotification().then(notif => {
...@@ -20,6 +27,7 @@ export default class PushController extends Component { ...@@ -20,6 +27,7 @@ export default class PushController extends Component {
this.refreshUnsubscribe = FCM.on("refreshToken", token => { this.refreshUnsubscribe = FCM.on("refreshToken", token => {
console.log("TOKEN (refreshUnsubscribe)", token); console.log("TOKEN (refreshUnsubscribe)", token);
this.props.onChangeToken(token);
}); });
} }
......
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