FirebaseClient.js 937 Bytes
Newer Older
renato's avatar
renato committed
1
import FirebaseConstants from "./FirebaseConstants";
Libin Lu's avatar
Libin Lu committed
2
import { 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 {

Libin Lu's avatar
Libin Lu committed
8
  async send(body, type) {
Dmitry's avatar
Dmitry committed
9
		if(FirebaseConstants.KEY === 'YOUR_API_KEY'){
Libin Lu's avatar
Libin Lu committed
10 11 12
			Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
			return;
		}
renato's avatar
renato committed
13 14 15 16 17
  	let headers = new Headers({
  		"Content-Type": "application/json",
      "Authorization": "key=" + FirebaseConstants.KEY
  	});

Libin Lu's avatar
Libin Lu committed
18 19
		try {
			let response = await fetch(API_URL, { method: "POST", headers, body });
Libin Lu's avatar
Libin Lu committed
20 21 22 23 24 25 26
			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
27 28 29 30 31
				Alert.alert('Failed to send notification, check error log')
			}
		} catch (err) {
			Alert.alert(err && err.message)
		}
renato's avatar
renato committed
32 33 34 35 36 37
  }

}

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