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";
import firebaseClient from "./FirebaseClient";
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
token: ""
}
}
render() {
let { token } = this.state;
return (
<View style={styles.container}>
<PushController />
<PushController
onChangeToken={token => this.setState({token: token || ""})}
/>
<Text style={styles.welcome}>
Welcome to Simple Fcm Client!
</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>
</TouchableOpacity>
<TouchableOpacity onPress={() => firebaseClient.sendData()} style={styles.button}>
<TouchableOpacity onPress={() => firebaseClient.sendData(token)} style={styles.button}>
<Text style={styles.buttonText}>Send Data</Text>
</TouchableOpacity>
</View>
......
......@@ -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
}
......
......@@ -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);
});
}
......
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