App.js 5.85 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'
    });
  }

Libin Lu's avatar
Libin Lu committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  sendRemoteNotification(token) {
    let body;

    if(Platform.OS === 'android'){
      body = {
        "to": token,
      	"data":{
					"custom_notification": {
						"title": "Simple FCM Client",
						"body": "This is a notification with only NOTIFICATION.",
						"sound": "default",
						"priority": "high",
						"show_in_foreground": true
        	}
    		},
    		"priority": 10
      }
    } else {
			body = {
				"to": token,
				"notification":{
					"title": "Simple FCM Client",
					"body": "This is a notification with only NOTIFICATION.",
					"sound": "default"
				},
				"priority": 10
			}
		}

    firebaseClient.send(JSON.stringify(body), "notification");
  }

  sendRemoteData(token) {
    let body = {
    	"to": token,
      "data":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with only DATA.",
    		"sound": "default"
    	},
    	"priority": "normal"
    }

    firebaseClient.send(JSON.stringify(body), "data");
  }

  sendRemoteNotificationWithData(token) {
    let body = {
      "to": token,
      "notification":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
				"sound": "default"
    	},
    	"data":{
    		"hello": "there"
    	},
    	"priority": "high"
    }

    firebaseClient.send(JSON.stringify(body), "notification-data");
  }

renato's avatar
renato committed
131
  render() {
132
    let { token, tokenCopyFeedback } = this.state;
133

renato's avatar
renato committed
134 135
    return (
      <View style={styles.container}>
136 137 138
        <PushController
          onChangeToken={token => this.setState({token: token || ""})}
        />
renato's avatar
renato committed
139 140 141 142
        <Text style={styles.welcome}>
          Welcome to Simple Fcm Client!
        </Text>

Libin Lu's avatar
Libin Lu committed
143 144 145 146 147
        <Text>
          Init notif: {JSON.stringify(this.state.initNotif)}

        </Text>

148
        <Text selectable={true} onPress={() => this.setClipboardContent(this.state.token)} style={styles.instructions}>
149 150 151
          Token: {this.state.token}
        </Text>

152 153 154 155
        <Text style={styles.feedback}>
          {this.state.tokenCopyFeedback}
        </Text>

Libin Lu's avatar
Libin Lu committed
156 157 158 159
        <Text style={styles.feedback}>
          Remote notif won't be available to iOS emulators
        </Text>

Libin Lu's avatar
Libin Lu committed
160 161
        <TouchableOpacity onPress={() => firebaseClient.sendRemoteNotification(token)} style={styles.button}>
          <Text style={styles.buttonText}>Send Remote Notification</Text>
renato's avatar
renato committed
162 163
        </TouchableOpacity>

Libin Lu's avatar
Libin Lu committed
164 165
        <TouchableOpacity onPress={() => firebaseClient.sendRemoteData(token)} style={styles.button}>
          <Text style={styles.buttonText}>Send Remote Data</Text>
renato's avatar
renato committed
166
        </TouchableOpacity>
renato's avatar
renato committed
167

Libin Lu's avatar
Libin Lu committed
168 169
        <TouchableOpacity onPress={() => firebaseClient.sendRemoteNotificationWithData(token)} style={styles.button}>
          <Text style={styles.buttonText}>Send Remote Notification With Data</Text>
renato's avatar
renato committed
170
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
171 172 173 174

        <TouchableOpacity onPress={() => this.showLocalNotification()} style={styles.button}>
          <Text style={styles.buttonText}>Send Local Notification</Text>
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
175 176 177 178

        <TouchableOpacity onPress={() => this.scheduleLocalNotification()} style={styles.button}>
          <Text style={styles.buttonText}>Schedule Notification in 5s</Text>
        </TouchableOpacity>
renato's avatar
renato committed
179 180 181
      </View>
    );
  }
182 183 184 185 186 187 188 189 190 191

  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
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

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',
209 210 211 212 213 214
    marginBottom: 2,
  },
  feedback: {
    textAlign: 'center',
    color: '#996633',
    marginBottom: 3,
renato's avatar
renato committed
215 216 217 218 219
  },
  button: {
    backgroundColor: "teal",
    paddingHorizontal: 20,
    paddingVertical: 10,
renato's avatar
renato committed
220
    marginVertical: 15,
renato's avatar
renato committed
221 222 223 224 225 226 227
    borderRadius: 10
  },
  buttonText: {
    color: "white",
    backgroundColor: "transparent"
  },
});