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

  sendNotification(token) {
renato's avatar
renato committed
15
    let body = {
16
    	"to": token,
renato's avatar
renato committed
17 18 19
      "notification":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with only NOTIFICATION.",
renato's avatar
renato committed
20 21 22 23 24
    		"sound": "default",
    		"click_action": "fcm.ACTION.HELLO"
    	},
    	"priority": 10
    }
Libin Lu's avatar
Libin Lu committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    
    if(Platform.OS === 'android'){
      body = {
        "to": token,
      "data":{
        "custom_notification": {
    		  "title": "Simple FCM Client",
    		  "body": "This is a notification with only NOTIFICATION.",
    		  "sound": "default",
    		  "click_action": "fcm.ACTION.HELLO"
        }
    	},
    	"priority": 10
      }
    }
renato's avatar
renato committed
40 41 42 43

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

44
  sendData(token) {
renato's avatar
renato committed
45
    let body = {
46
    	"to": token,
renato's avatar
renato committed
47
      "data":{
renato's avatar
renato committed
48 49
    		"title": "Simple FCM Client",
    		"body": "This is a notification with only DATA.",
renato's avatar
renato committed
50
    		"sound": "default",
renato's avatar
renato committed
51 52
    		"click_action": "fcm.ACTION.HELLO",
    		"remote": true
renato's avatar
renato committed
53
    	},
renato's avatar
renato committed
54
    	"priority": "normal"
renato's avatar
renato committed
55 56 57 58 59
    }

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

renato's avatar
renato committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  sendNotificationWithData(token) {
    let body = {
      "to": token,
      "notification":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
    		"sound": "default",
    		"click_action": "fcm.ACTION.HELLO"
    	},
    	"data":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with NOTIFICATION and DATA (DATA)",
    		"click_action": "fcm.ACTION.HELLO",
    		"remote": true
    	},
    	"priority": "high"
    }

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

renato's avatar
renato committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  _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;