FirebaseClient.js 1.44 KB
Newer Older
renato's avatar
renato committed
1 2 3 4 5 6
import FirebaseConstants from "./FirebaseConstants";

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

class FirebaseClient {

7 8 9 10 11 12
  constructor() {
    this.sendData = this.sendData.bind(this);
    this.sendNotification = this.sendNotification.bind(this);
  }

  sendNotification(token) {
renato's avatar
renato committed
13
    let body = {
14
    	"to": token,
renato's avatar
renato committed
15 16 17 18 19 20 21
    	"notification":{
    		"icon": "appLogo",
    		"title": "Notification Title",
    		"body": "Notification Body",
    		"sound": "default",
    		"click_action": "fcm.ACTION.HELLO"
    	},
22
      "content_available": true,
renato's avatar
renato committed
23 24 25 26 27 28
    	"priority": 10
    }

    this._send(JSON.stringify(body), "notification");
  }

29
  sendData(token) {
renato's avatar
renato committed
30
    let body = {
31
    	"to": token,
renato's avatar
renato committed
32 33 34 35 36 37 38
      "data":{
    		"icon": "appLogo",
    		"title": "Notification Title",
    		"body": "Notification Body",
    		"sound": "default",
    		"click_action": "fcm.ACTION.HELLO"
    	},
39
      "content_available": true,
renato's avatar
renato committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    	"priority": 10
    }

    this._send(JSON.stringify(body), "data");
  }

  _send(body, type) {
  	let headers = new Headers({
  		"Content-Type": "application/json",
  		"Content-Length": parseInt(body.length),
      "Authorization": "key=" + FirebaseConstants.KEY
  	});

  	fetch(API_URL, { method: "POST", headers, body })
  		.then(response => console.log("Send " + type + " response", response))
  		.catch(error => console.log("Error sending " + type, error));
  }

}

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