import FirebaseConstants from "./FirebaseConstants";
import { Platform, Alert } from "react-native";

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

class FirebaseClient {

  constructor() {
    this.sendData = this.sendData.bind(this);
    this.sendNotification = this.sendNotification.bind(this);
    this.sendNotificationWithData = this.sendNotificationWithData.bind(this);
  }

  sendNotification(token) {
    let body = {
    	"to": token,
      "notification":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with only NOTIFICATION.",
    		"sound": "default"
    	},
    	"priority": 10
    }

    if(Platform.OS === 'android'){
      body = {
        "to": token,
      "data":{
        "custom_notification": {
    		  "title": "Simple FCM Client",
    		  "body": "This is a notification with only NOTIFICATION.",
    		  "sound": "default",
		"priority": "high"
        }
    	},
    	"priority": 10
      }
    }

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

  sendData(token) {
    let body = {
    	"to": token,
      "data":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with only DATA.",
    		"sound": "default"
    	},
    	"priority": "normal"
    }

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

  sendNotificationWithData(token) {
    let body = {
      "to": token,
      "notification":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
				"sound": "default"
    	},
    	"data":{
    		"hello": "there"
    	},
    	"priority": "high"
    }

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

  async _send(body, type) {
		if(FirebaseClient.KEY === 'YOUR_API_KEY'){
			Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
			return;
		}
  	let headers = new Headers({
  		"Content-Type": "application/json",
      "Authorization": "key=" + FirebaseConstants.KEY
  	});

		try {
			let response = await fetch(API_URL, { method: "POST", headers, body });
			console.log(response);
			try{
				response = await response.json();
				if(!response.success){
					Alert.alert('Failed to send notification, check error log')
				}
			} catch (err){
				Alert.alert('Failed to send notification, check error log')
			}
		} catch (err) {
			Alert.alert(err && err.message)
		}
  }

}

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

@@ -428,38 +428,111 @@
 body
- = %7B%0A    %09%22to%22: token,%0A      %22
+;%0A%0A    if(Platform.OS === 'android')%7B%0A      body = %7B%0A        %22to%22: token,%0A      %09%22data%22:%7B%0A%09%09%09%09%09%22custom_
 noti
@@ -533,38 +533,39 @@
 m_notification%22:
+ 
 %7B%0A
-    
+%09%09%09%09
 %09%09%22title%22: %22Simp
@@ -572,36 +572,36 @@
 le FCM Client%22,%0A
-    
+%09%09%09%09
 %09%09%22body%22: %22This 
@@ -640,28 +640,28 @@
 FICATION.%22,%0A
-    
+%09%09%09%09
 %09%09%22sound%22: %22
@@ -660,37 +660,109 @@
 ound%22: %22default%22
-%0A    
+,%0A%09%09%09%09%09%09%22priority%22: %22high%22,%0A%09%09%09%09%09%09%22show_in_foreground%22: true%0A        %09%7D%0A    %09
 %09%7D,%0A    %09%22priori
@@ -746,32 +746,33 @@
 %7D%0A    %09%09%7D,%0A    %09
+%09
 %22priority%22: 10%0A 
@@ -778,52 +778,28 @@
     
+ 
+ 
 %7D
-%0A
 %0A    
-if(Platform.OS === 'android')%7B%0A      
+%7D else %7B%0A%09%09%09
 body
@@ -799,32 +799,28 @@
 %09%09%09body = %7B%0A
-        
+%09%09%09%09
 %22to%22: token,
@@ -824,39 +824,13 @@
 en,%0A
-      %22data%22:%7B%0A        %22custom_
+%09%09%09%09%22
 noti
@@ -843,19 +843,15 @@
 on%22:
- 
 %7B%0A
-    %09%09  
+%09%09%09%09%09
 %22tit
@@ -876,24 +876,21 @@
 lient%22,%0A
-    %09%09  
+%09%09%09%09%09
 %22body%22: 
@@ -943,16 +943,13 @@
 .%22,%0A
-    %09%09  
+%09%09%09%09%09
 %22sou
@@ -966,53 +966,19 @@
 ult%22
-,
 %0A%09%09
-%22priority%22: %22high%22%0A        %7D%0A    %09%7D,%0A    
+%09%09%7D,%0A%09%09%09
 %09%22pr
@@ -989,28 +989,23 @@
 ty%22: 10%0A
-      %7D%0A    
+%09%09%09%7D%0A%09%09
 %7D%0A%0A    t

update example