App.js 4.47 KB
Newer Older
renato's avatar
renato committed
1 2 3 4 5 6 7 8 9 10 11
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  TouchableOpacity,
12 13
  View,
  Clipboard
renato's avatar
renato committed
14 15
} from 'react-native';

Libin Lu's avatar
Libin Lu committed
16 17
import FCM from "react-native-fcm";

renato's avatar
renato committed
18 19 20 21
import PushController from "./PushController";
import firebaseClient from  "./FirebaseClient";

export default class App extends Component {
22 23 24 25
  constructor(props) {
    super(props);

    this.state = {
26 27
      token: "",
      tokenCopyFeedback: ""
28 29 30
    }
  }

Libin Lu's avatar
Libin Lu committed
31 32 33 34 35 36 37 38
  componentDidMount(){
    FCM.getInitialNotification().then(notif => {
      this.setState({
        initNotif: notif
      })
    });
  }

Libin Lu's avatar
Libin Lu committed
39 40
  showLocalNotification() {
    FCM.presentLocalNotification({
Libin Lu's avatar
Libin Lu committed
41
      vibrate: 500,
Libin Lu's avatar
Libin Lu committed
42 43
      title: 'Hello',
      body: 'Test Notification',
Libin Lu's avatar
Libin Lu committed
44
      big_text: 'i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large, i am large',
Libin Lu's avatar
Libin Lu committed
45
      priority: "high",
Libin Lu's avatar
Libin Lu committed
46
      sound: "bell.mp3",
Libin Lu's avatar
Libin Lu committed
47
      large_icon: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg",
Libin Lu's avatar
Libin Lu committed
48 49
      show_in_foreground: true,
      number: 10
Libin Lu's avatar
Libin Lu committed
50 51 52
    });
  }

Libin Lu's avatar
Libin Lu committed
53 54 55 56 57 58 59
  scheduleLocalNotification() {
    FCM.scheduleLocalNotification({
      id: 'testnotif',
      fire_date: new Date().getTime()+5000,
      vibrate: 500,
      title: 'Hello',
      body: 'Test Scheduled Notification',
Libin Lu's avatar
Libin Lu committed
60
      sub_text: 'sub text',
Libin Lu's avatar
Libin Lu committed
61
      priority: "high",
Libin Lu's avatar
Libin Lu committed
62
      large_icon: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg",
Libin Lu's avatar
Libin Lu committed
63 64 65 66 67
      show_in_foreground: true,
      picture: 'https://firebase.google.com/_static/af7ae4b3fc/images/firebase/lockup.png'
    });
  }

renato's avatar
renato committed
68
  render() {
69
    let { token, tokenCopyFeedback } = this.state;
70

renato's avatar
renato committed
71 72
    return (
      <View style={styles.container}>
73 74 75
        <PushController
          onChangeToken={token => this.setState({token: token || ""})}
        />
renato's avatar
renato committed
76 77 78 79
        <Text style={styles.welcome}>
          Welcome to Simple Fcm Client!
        </Text>

Libin Lu's avatar
Libin Lu committed
80 81 82 83 84
        <Text>
          Init notif: {JSON.stringify(this.state.initNotif)}

        </Text>

85
        <Text selectable={true} onPress={() => this.setClipboardContent(this.state.token)} style={styles.instructions}>
86 87 88
          Token: {this.state.token}
        </Text>

89 90 91 92
        <Text style={styles.feedback}>
          {this.state.tokenCopyFeedback}
        </Text>

Libin Lu's avatar
Libin Lu committed
93 94 95 96
        <Text style={styles.feedback}>
          Remote notif won't be available to iOS emulators
        </Text>

97
        <TouchableOpacity onPress={() => firebaseClient.sendNotification(token)} style={styles.button}>
renato's avatar
renato committed
98 99 100
          <Text style={styles.buttonText}>Send Notification</Text>
        </TouchableOpacity>

101
        <TouchableOpacity onPress={() => firebaseClient.sendData(token)} style={styles.button}>
renato's avatar
renato committed
102 103
          <Text style={styles.buttonText}>Send Data</Text>
        </TouchableOpacity>
renato's avatar
renato committed
104 105 106 107

        <TouchableOpacity onPress={() => firebaseClient.sendNotificationWithData(token)} style={styles.button}>
          <Text style={styles.buttonText}>Send Notification With Data</Text>
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
108 109 110 111

        <TouchableOpacity onPress={() => this.showLocalNotification()} style={styles.button}>
          <Text style={styles.buttonText}>Send Local Notification</Text>
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
112 113 114 115

        <TouchableOpacity onPress={() => this.scheduleLocalNotification()} style={styles.button}>
          <Text style={styles.buttonText}>Schedule Notification in 5s</Text>
        </TouchableOpacity>
renato's avatar
renato committed
116 117 118
      </View>
    );
  }
119 120 121 122 123 124 125 126 127 128

  setClipboardContent(text) {
    Clipboard.setString(text);
    this.setState({tokenCopyFeedback: "Token copied to clipboard."});
    setTimeout(() => {this.clearTokenCopyFeedback()}, 2000);
  }

  clearTokenCopyFeedback() {
    this.setState({tokenCopyFeedback: ""});
  }
renato's avatar
renato committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
146 147 148 149 150 151
    marginBottom: 2,
  },
  feedback: {
    textAlign: 'center',
    color: '#996633',
    marginBottom: 3,
renato's avatar
renato committed
152 153 154 155 156
  },
  button: {
    backgroundColor: "teal",
    paddingHorizontal: 20,
    paddingVertical: 10,
renato's avatar
renato committed
157
    marginVertical: 15,
renato's avatar
renato committed
158 159 160 161 162 163 164
    borderRadius: 10
  },
  buttonText: {
    color: "white",
    backgroundColor: "transparent"
  },
});