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

Libin Lu's avatar
Libin Lu committed
3
import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from "react-native-fcm";
renato's avatar
renato committed
4

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
    });

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

Libin Lu's avatar
Libin Lu committed
24
    this.notificationListner = FCM.on(FCMEvent.Notification, notif => {
renato's avatar
renato committed
25
      console.log("Notification", notif);
Libin Lu's avatar
Libin Lu committed
26
      if(notif.local_notification){
renato's avatar
renato committed
27 28
        return;
      }
Libin Lu's avatar
Libin Lu committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      if(notif.opened_from_tray){
        return;
      }

      if(Platform.OS ==='ios'){
              //optional
              //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
              //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
              //notif._notificationType is available for iOS platfrom
              switch(notif._notificationType){
                case NotificationType.Remote:
                  notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
                  break;
                case NotificationType.NotificationResponse:
                  notif.finish();
                  break;
                case NotificationType.WillPresent:
                  notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
                  break;
              }
            }
      this.showLocalNotification(notif);
renato's avatar
renato committed
51 52
    });

Libin Lu's avatar
Libin Lu committed
53
    this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => {
renato's avatar
renato committed
54
      console.log("TOKEN (refreshUnsubscribe)", token);
55
      this.props.onChangeToken(token);
renato's avatar
renato committed
56 57 58
    });
  }

Libin Lu's avatar
Libin Lu committed
59
  showLocalNotification(notif) {
renato's avatar
renato committed
60 61 62 63 64 65 66 67 68 69
    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
70
  componentWillUnmount() {
Libin Lu's avatar
Libin Lu committed
71 72
    this.notificationListner.remove();
    this.refreshTokenListener.remove();
renato's avatar
renato committed
73 74 75 76 77 78 79
  }


  render() {
    return null;
  }
}