PushController.js 1.25 KB
Newer Older
renato's avatar
renato committed
1 2 3 4
import React, { Component } from "react";

import FCM from "react-native-fcm";

5 6
import firebaseClient from  "./FirebaseClient";

renato's avatar
renato committed
7
export default class PushController extends Component {
8 9 10 11
  constructor(props) {
    super(props);
  }

renato's avatar
renato committed
12 13 14 15 16
  componentDidMount() {
    FCM.requestPermissions();

    FCM.getFCMToken().then(token => {
      console.log("TOKEN (getFCMToken)", token);
17
      this.props.onChangeToken(token);
renato's avatar
renato committed
18 19 20 21 22 23 24 25
    });

    FCM.getInitialNotification().then(notif => {
      console.log("INITIAL NOTIFICATION", notif)
    });

    this.notificationUnsubscribe = FCM.on("notification", notif => {
      console.log("Notification", notif);
renato's avatar
renato committed
26 27 28 29
      if (notif && notif.local) {
        return;
      }
      this.sendRemote(notif);
renato's avatar
renato committed
30 31 32 33
    });

    this.refreshUnsubscribe = FCM.on("refreshToken", token => {
      console.log("TOKEN (refreshUnsubscribe)", token);
34
      this.props.onChangeToken(token);
renato's avatar
renato committed
35 36 37
    });
  }

renato's avatar
renato committed
38 39 40 41 42 43 44 45 46 47 48
  sendRemote(notif) {
    FCM.presentLocalNotification({
      title: notif.title,
      body: notif.body,
      priority: "high",
      click_action: notif.click_action,
      show_in_foreground: true,
      local: true
    });
  }

renato's avatar
renato committed
49 50 51 52 53 54 55 56 57 58
  componentWillUnmount() {
    this.refreshUnsubscribe();
    this.notificationUnsubscribe();
  }


  render() {
    return null;
  }
}