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

3 4
import { Platform } from 'react-native';

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

7 8
import firebaseClient from  "./FirebaseClient";

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

Libin Lu's avatar
Libin Lu committed
14 15 16 17 18 19 20
  async componentDidMount() {

    try{
      let result = await FCM.requestPermissions({badge: false, sound: true, alert: true});
    } catch(e){
      console.error(e);
    }
renato's avatar
renato committed
21 22 23

    FCM.getFCMToken().then(token => {
      console.log("TOKEN (getFCMToken)", token);
24
      this.props.onChangeToken(token);
renato's avatar
renato committed
25
    });
Libin Lu's avatar
Libin Lu committed
26 27 28 29 30 31

    if(Platform.OS === 'ios'){
      FCM.getAPNSToken().then(token => {
        console.log("APNS TOKEN (getFCMToken)", token);
      });
    }
renato's avatar
renato committed
32 33 34 35 36

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

37
    this.notificationListener = FCM.on(FCMEvent.Notification, notif => {
renato's avatar
renato committed
38
      console.log("Notification", notif);
Libin Lu's avatar
Libin Lu committed
39
      if(notif.local_notification){
renato's avatar
renato committed
40 41
        return;
      }
Libin Lu's avatar
Libin Lu committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      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;
              }
Libin Lu's avatar
Libin Lu committed
62
      }
Libin Lu's avatar
Libin Lu committed
63

Libin Lu's avatar
Libin Lu committed
64 65 66 67
      this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => {
        console.log("TOKEN (refreshUnsubscribe)", token);
        this.props.onChangeToken(token);
      });
renato's avatar
renato committed
68

Libin Lu's avatar
Libin Lu committed
69 70 71 72 73 74 75 76 77 78
      // direct channel related methods are ios only
      // directly channel is truned off in iOS by default, this method enables it
      FCM.enableDirectChannel();
      this.channelConnectionListener = FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => {
        console.log('direct channel connected' + data);
      });
      setTimeout(function() {
        FCM.isDirectChannelEstablished().then(d => console.log(d));
      }, 1000);
    })
renato's avatar
renato committed
79 80
  }

renato's avatar
renato committed
81
  componentWillUnmount() {
82
    this.notificationListener.remove();
Libin Lu's avatar
Libin Lu committed
83
    this.refreshTokenListener.remove();
renato's avatar
renato committed
84 85 86 87 88 89 90
  }


  render() {
    return null;
  }
}