FirebaseClient.js 2.29 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 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.",
Libin Lu's avatar
Libin Lu committed
20
    		"sound": "default"
renato's avatar
renato committed
21 22 23
    	},
    	"priority": 10
    }
Libin Lu's avatar
Libin Lu committed
24

Libin Lu's avatar
Libin Lu committed
25 26 27 28 29 30 31 32
    if(Platform.OS === 'android'){
      body = {
        "to": token,
      "data":{
        "custom_notification": {
    		  "title": "Simple FCM Client",
    		  "body": "This is a notification with only NOTIFICATION.",
    		  "sound": "default",
Libin Lu's avatar
Libin Lu committed
33
		"priority": "high"
Libin Lu's avatar
Libin Lu committed
34 35 36 37 38
        }
    	},
    	"priority": 10
      }
    }
renato's avatar
renato committed
39 40 41 42

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

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

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

renato's avatar
renato committed
57 58 59 60 61 62
  sendNotificationWithData(token) {
    let body = {
      "to": token,
      "notification":{
    		"title": "Simple FCM Client",
    		"body": "This is a notification with NOTIFICATION and DATA (NOTIF).",
Libin Lu's avatar
Libin Lu committed
63
				"sound": "default"
renato's avatar
renato committed
64 65
    	},
    	"data":{
Libin Lu's avatar
Libin Lu committed
66
    		"hello": "there"
renato's avatar
renato committed
67 68 69 70 71 72 73
    	},
    	"priority": "high"
    }

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

Libin Lu's avatar
Libin Lu committed
74 75 76 77 78
  async _send(body, type) {
		if(FirebaseClient.KEY === 'YOUR_API_KEY'){
			Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
			return;
		}
renato's avatar
renato committed
79 80 81 82 83
  	let headers = new Headers({
  		"Content-Type": "application/json",
      "Authorization": "key=" + FirebaseConstants.KEY
  	});

Libin Lu's avatar
Libin Lu committed
84 85 86 87 88 89
		try {
			let response = await fetch(API_URL, { method: "POST", headers, body });
			response = await response.json();
			if(!response.success){
				Alert.alert('Failed to send notification, check error log')
			}
Libin Lu's avatar
Libin Lu committed
90
			console.log(response);
Libin Lu's avatar
Libin Lu committed
91 92 93
		} catch (err) {
			Alert.alert(err && err.message)
		}
renato's avatar
renato committed
94 95 96 97 98 99
  }

}

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