App.js 6.66 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
  View,
Libin Lu's avatar
Libin Lu committed
13 14
  Clipboard,
  Platform
renato's avatar
renato committed
15 16
} from 'react-native';

Libin Lu's avatar
Libin Lu committed
17 18
import { StackNavigator } from 'react-navigation';

Libin Lu's avatar
Libin Lu committed
19 20
import FCM from "react-native-fcm";

Libin Lu's avatar
Libin Lu committed
21
import {registerKilledListener, registerAppListener} from "./Listeners";
renato's avatar
renato committed
22 23
import firebaseClient from  "./FirebaseClient";

Libin Lu's avatar
Libin Lu committed
24 25
registerKilledListener();

Libin Lu's avatar
Libin Lu committed
26
class MainPage extends Component {
27 28 29 30
  constructor(props) {
    super(props);

    this.state = {
31 32
      token: "",
      tokenCopyFeedback: ""
33 34 35
    }
  }

Libin Lu's avatar
Libin Lu committed
36
  async componentDidMount(){
Libin Lu's avatar
Libin Lu committed
37
    registerAppListener(this.props.navigation);
Libin Lu's avatar
Libin Lu committed
38 39 40 41
    FCM.getInitialNotification().then(notif => {
      this.setState({
        initNotif: notif
      })
Libin Lu's avatar
Libin Lu committed
42 43 44 45 46
      if(notif.targetScreen === 'detail'){
        setTimeout(()=>{
          this.props.navigation.navigate('Detail')
        }, 500)
      }
Libin Lu's avatar
Libin Lu committed
47
    });
Libin Lu's avatar
Libin Lu committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    try{
      let result = await FCM.requestPermissions({badge: false, sound: true, alert: true});
    } catch(e){
      console.error(e);
    }

    FCM.getFCMToken().then(token => {
      console.log("TOKEN (getFCMToken)", token);
      this.setState({token: token || ""})
    });

    if(Platform.OS === 'ios'){
      FCM.getAPNSToken().then(token => {
        console.log("APNS TOKEN (getFCMToken)", token);
      });
    }
Libin Lu's avatar
Libin Lu committed
65 66
  }

Libin Lu's avatar
Libin Lu committed
67 68
  showLocalNotification() {
    FCM.presentLocalNotification({
Libin Lu's avatar
Libin Lu committed
69
      vibrate: 500,
Libin Lu's avatar
Libin Lu committed
70
      title: 'Hello',
Libin Lu's avatar
Libin Lu committed
71 72
      body: 'Click me to detail page,',
      big_text: 'Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text ',
Libin Lu's avatar
Libin Lu committed
73
      priority: "high",
Libin Lu's avatar
Libin Lu committed
74
      sound: "bell.mp3",
Libin Lu's avatar
Libin Lu committed
75
      large_icon: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg",
Libin Lu's avatar
Libin Lu committed
76
      show_in_foreground: true,
Libin Lu's avatar
Libin Lu committed
77
      group: 'test',
Libin Lu's avatar
Libin Lu committed
78
      targetScreen: 'detail',
Libin Lu's avatar
Libin Lu committed
79
      number: 10
Libin Lu's avatar
Libin Lu committed
80 81 82
    });
  }

Libin Lu's avatar
Libin Lu committed
83 84 85 86 87 88 89
  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
90
      sub_text: 'sub text',
Libin Lu's avatar
Libin Lu committed
91
      priority: "high",
Libin Lu's avatar
Libin Lu committed
92
      large_icon: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg",
Libin Lu's avatar
Libin Lu committed
93
      show_in_foreground: true,
Libin Lu's avatar
Libin Lu committed
94
      picture: 'https://firebase.google.com/_static/af7ae4b3fc/images/firebase/lockup.png',
Libin Lu's avatar
Libin Lu committed
95 96 97
      wake_screen: true,
      extra1: {a: 1},
      extra2: 1
Libin Lu's avatar
Libin Lu committed
98 99 100
    });
  }

Libin Lu's avatar
Libin Lu committed
101 102 103 104 105 106 107 108 109
  sendRemoteNotification(token) {
    let body;

    if(Platform.OS === 'android'){
      body = {
        "to": token,
      	"data":{
					"custom_notification": {
						"title": "Simple FCM Client",
Libin Lu's avatar
Libin Lu committed
110
						"body": "Click me to go to detail",
Libin Lu's avatar
Libin Lu committed
111 112
						"sound": "default",
						"priority": "high",
Libin Lu's avatar
Libin Lu committed
113 114
            "show_in_foreground": true,
            targetScreen: 'detail'
Libin Lu's avatar
Libin Lu committed
115 116 117 118 119 120 121 122 123
        	}
    		},
    		"priority": 10
      }
    } else {
			body = {
				"to": token,
				"notification":{
					"title": "Simple FCM Client",
Libin Lu's avatar
Libin Lu committed
124
					"body": "Click me to go to detail",
Libin Lu's avatar
Libin Lu committed
125
					"sound": "default"
Libin Lu's avatar
Libin Lu committed
126 127 128 129
        },
        data: {
          targetScreen: 'detail'
        },
Libin Lu's avatar
Libin Lu committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
				"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");
  }

Libin Lu's avatar
Libin Lu committed
151 152 153 154 155 156 157 158
  showLocalNotificationWithAction() {
    FCM.presentLocalNotification({
      title: 'Test Notification with action',
      body: 'Force touch to reply',
      priority: "high",
      show_in_foreground: true,
      click_action: "com.myidentifi.fcm.text"
    });
Libin Lu's avatar
Libin Lu committed
159 160
  }

renato's avatar
renato committed
161
  render() {
162
    let { token, tokenCopyFeedback } = this.state;
163

renato's avatar
renato committed
164 165 166 167 168 169
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to Simple Fcm Client!
        </Text>

170 171 172 173
        <Text style={styles.feedback}>
          {this.state.tokenCopyFeedback}
        </Text>

Libin Lu's avatar
Libin Lu committed
174 175 176 177
        <Text style={styles.feedback}>
          Remote notif won't be available to iOS emulators
        </Text>

Libin Lu's avatar
Libin Lu committed
178
        <TouchableOpacity onPress={() => this.sendRemoteNotification(token)} style={styles.button}>
Libin Lu's avatar
Libin Lu committed
179
          <Text style={styles.buttonText}>Send Remote Notification</Text>
renato's avatar
renato committed
180 181
        </TouchableOpacity>

Libin Lu's avatar
Libin Lu committed
182
        <TouchableOpacity onPress={() => this.sendRemoteData(token)} style={styles.button}>
Libin Lu's avatar
Libin Lu committed
183
          <Text style={styles.buttonText}>Send Remote Data</Text>
renato's avatar
renato committed
184
        </TouchableOpacity>
renato's avatar
renato committed
185

Libin Lu's avatar
Libin Lu committed
186 187
        <TouchableOpacity onPress={() => this.showLocalNotification()} style={styles.button}>
          <Text style={styles.buttonText}>Show Local Notification</Text>
renato's avatar
renato committed
188
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
189

Libin Lu's avatar
Libin Lu committed
190
        <TouchableOpacity onPress={() => this.showLocalNotificationWithAction(token)} style={styles.button}>
Libin Lu's avatar
Libin Lu committed
191
          <Text style={styles.buttonText}>Show Local Notification with Action (iOS)</Text>
Libin Lu's avatar
Libin Lu committed
192
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
193 194 195 196

        <TouchableOpacity onPress={() => this.scheduleLocalNotification()} style={styles.button}>
          <Text style={styles.buttonText}>Schedule Notification in 5s</Text>
        </TouchableOpacity>
Libin Lu's avatar
Libin Lu committed
197 198 199 200 201 202 203 204 205

        <Text>
          Init notif: {JSON.stringify(this.state.initNotif)}
        </Text>

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

renato's avatar
renato committed
206 207 208
      </View>
    );
  }
209 210 211 212 213 214 215 216 217 218

  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
219 220
}

Libin Lu's avatar
Libin Lu committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
class DetailPage extends Component {
  render(){
    return <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Detail page</Text>
    </View>
  }
}

export default StackNavigator({
  Main: {
    screen: MainPage,
  },
  Detail: {
    screen: DetailPage
  }
}, {
  initialRouteName: 'Main',
});

renato's avatar
renato committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
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',
255 256 257 258 259 260
    marginBottom: 2,
  },
  feedback: {
    textAlign: 'center',
    color: '#996633',
    marginBottom: 3,
renato's avatar
renato committed
261 262 263 264 265
  },
  button: {
    backgroundColor: "teal",
    paddingHorizontal: 20,
    paddingVertical: 10,
renato's avatar
renato committed
266
    marginVertical: 15,
renato's avatar
renato committed
267 268 269 270 271 272 273
    borderRadius: 10
  },
  buttonText: {
    color: "white",
    backgroundColor: "transparent"
  },
});