FirebaseClient.js 1.13 KB
Newer Older
renato's avatar
renato committed
1
import FirebaseConstants from "./FirebaseConstants";
Libin Lu's avatar
Libin Lu committed
2
import { Platform, Alert } from "react-native";
renato's avatar
renato committed
3 4 5 6 7

const API_URL = "https://fcm.googleapis.com/fcm/send";

class FirebaseClient {

8 9 10
  constructor() {
    this.sendData = this.sendData.bind(this);
    this.sendNotification = this.sendNotification.bind(this);
renato's avatar
renato committed
11
    this.sendNotificationWithData = this.sendNotificationWithData.bind(this);
12 13
  }

Libin Lu's avatar
Libin Lu committed
14
  async send(body, type) {
Libin Lu's avatar
Libin Lu committed
15 16 17 18
		if(FirebaseClient.KEY === 'YOUR_API_KEY'){
			Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
			return;
		}
renato's avatar
renato committed
19 20 21 22 23
  	let headers = new Headers({
  		"Content-Type": "application/json",
      "Authorization": "key=" + FirebaseConstants.KEY
  	});

Libin Lu's avatar
Libin Lu committed
24 25
		try {
			let response = await fetch(API_URL, { method: "POST", headers, body });
Libin Lu's avatar
Libin Lu committed
26 27 28 29 30 31 32
			console.log(response);
			try{
				response = await response.json();
				if(!response.success){
					Alert.alert('Failed to send notification, check error log')
				}
			} catch (err){
Libin Lu's avatar
Libin Lu committed
33 34 35 36 37
				Alert.alert('Failed to send notification, check error log')
			}
		} catch (err) {
			Alert.alert(err && err.message)
		}
renato's avatar
renato committed
38 39 40 41 42 43
  }

}

let firebaseClient = new FirebaseClient();
export default firebaseClient;