FirebaseClient.js 2.43 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) {
Libin Lu's avatar
Libin Lu committed
15
    let body;
Libin Lu's avatar
Libin Lu committed
16

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

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

46
  sendData(token) {
renato's avatar
renato committed
47
    let body = {
48
    	"to": token,
renato's avatar
renato committed
49
      "data":{
renato's avatar
renato committed
50 51
    		"title": "Simple FCM Client",
    		"body": "This is a notification with only DATA.",
Libin Lu's avatar
Libin Lu committed
52
    		"sound": "default"
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
  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
66
				"sound": "default"
renato's avatar
renato committed
67 68
    	},
    	"data":{
Libin Lu's avatar
Libin Lu committed
69
    		"hello": "there"
renato's avatar
renato committed
70 71 72 73 74 75 76
    	},
    	"priority": "high"
    }

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

Libin Lu's avatar
Libin Lu committed
77 78 79 80 81
  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
82 83 84 85 86
  	let headers = new Headers({
  		"Content-Type": "application/json",
      "Authorization": "key=" + FirebaseConstants.KEY
  	});

Libin Lu's avatar
Libin Lu committed
87 88
		try {
			let response = await fetch(API_URL, { method: "POST", headers, body });
Libin Lu's avatar
Libin Lu committed
89 90 91 92 93 94 95
			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
96 97 98 99 100
				Alert.alert('Failed to send notification, check error log')
			}
		} catch (err) {
			Alert.alert(err && err.message)
		}
renato's avatar
renato committed
101 102 103 104 105 106
  }

}

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