SHARE
TWEET

splashScreen.js

a guest Mar 26th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { PureComponent } from "react";
  2. import {
  3.   InteractionManager,
  4.   StatusBar,
  5.   Text,
  6.   View,
  7.   AsyncStorage
  8. } from "react-native";
  9. import firebase from "react-native-firebase";
  10. import { NavigationActions, StackActions } from "react-navigation";
  11. import { connect } from "react-redux";
  12. import { setFcmToken } from "../actions/auth";
  13. import globalStyles from "../assets/styles/global";
  14. import { setStatusInbox } from "../actions/notification";
  15.  
  16. class SplashScreen extends PureComponent {
  17.   static navigationOptions = {
  18.     header: null
  19.   };
  20.  
  21.   state = {
  22.     ready: false
  23.   };
  24.  
  25.   componentDidMount() {
  26.     this.checkPermission();
  27.     this.createNotificationListeners();
  28.  
  29.     InteractionManager.runAfterInteractions(() => {
  30.       this.setState({ ready: true });
  31.       if (this.props.isLogin) {
  32.         return this.onResetNav("Home");
  33.       } else {
  34.         return this.onResetNav("Login");
  35.       }
  36.     });
  37.   }
  38.  
  39.   async checkPermission() {
  40.     const enabled = await firebase.messaging().hasPermission();
  41.     if (enabled) {
  42.       this.getToken();
  43.     } else {
  44.       this.requestPermission();
  45.     }
  46.   }
  47.  
  48.   async getToken() {
  49.     let fcmToken = await AsyncStorage.getItem("fcmToken");
  50.     if (!fcmToken) {
  51.       fcmToken = await firebase.messaging().getToken();
  52.       if (fcmToken) {
  53.         await AsyncStorage.setItem("fcmToken", fcmToken);
  54.         this.props.setFcmToken(fcmToken);
  55.       }
  56.     }
  57.     this.props.setFcmToken(fcmToken);
  58.   }
  59.  
  60.   async requestPermission() {
  61.     try {
  62.       await firebase.messaging().requestPermission();
  63.       // User has authorised
  64.       this.getToken();
  65.     } catch (error) {
  66.       // User has rejected permissions
  67.       console.log("error", error);
  68.       console.log("permission rejected");
  69.     }
  70.   }
  71.  
  72.   componentWillUnmount() {
  73.     // this.messageListener();
  74.   }
  75.  
  76.   async createNotificationListeners() {
  77.     this.notificationListener = firebase
  78.       .notifications()
  79.       .onNotification(notifications => {
  80.         const { title, body } = notifications;
  81.  
  82.         const notification = new firebase.notifications.Notification()
  83.           .setNotificationId("notificationId")
  84.           .setTitle(title)
  85.           .setBody(body)
  86.           .android.setChannelId("channelId")
  87.           .android.setSmallIcon("ic_launcher")
  88.           .setSound("default");
  89.  
  90.         firebase.notifications().displayNotification(notification);
  91.  
  92.         this.props.setStatusInbox(true);
  93.       });
  94.  
  95.     this.notificationOpenedListener = firebase
  96.       .notifications()
  97.       .onNotificationOpened(notificationOpen => {
  98.         const { title, body } = notificationOpen.notification;
  99.         const id = 138;
  100.  
  101.         this.props.navigation.navigate("DetailOrder", {
  102.           id,
  103.           onNavigateBack: () => this.refresh(),
  104.           status: "notif"
  105.         });
  106.         // this.props.navigation.push("Inbox");
  107.         console.log("notif", this.props);
  108.       });
  109.  
  110.     const notificationOpen = await firebase
  111.       .notifications()
  112.       .getInitialNotification();
  113.     if (notificationOpen) {
  114.       const { title, body } = notificationOpen.notification;
  115.     }
  116.  
  117.     this.messageListener = firebase.messaging().onMessage(message => {
  118.       console.log(JSON.stringify(message));
  119.     });
  120.   }
  121.  
  122.   onResetNav(routeName) {
  123.     const resetAction = StackActions.reset({
  124.       index: 0,
  125.       actions: [NavigationActions.navigate({ routeName: routeName })]
  126.     });
  127.     this.props.navigation.dispatch(resetAction);
  128.   }
  129.  
  130.   render() {
  131.     if (!this.state.ready || this.state.ready == null) {
  132.       return <View style={globalStyles.alignCenterContainer} />;
  133.     }
  134.     return (
  135.       <View
  136.         style={[globalStyles.centerContainer, { backgroundColor: "#2845b5" }]}
  137.       >
  138.         <StatusBar hidden />
  139.         <Text
  140.           style={[
  141.             globalStyles.font,
  142.             { fontWeight: "bold", fontSize: 20, color: "white" }
  143.           ]}
  144.         >
  145.           Abunawas
  146.         </Text>
  147.         <Text style={[globalStyles.font, { color: "white" }]}>Corporate</Text>
  148.       </View>
  149.     );
  150.   }
  151. }
  152.  
  153. const mapStateToProps = ({ auth }) => ({
  154.   isLogin: auth.isLogin
  155. });
  156.  
  157. const mapDispatchToProps = dispatch => ({
  158.   setFcmToken: token => dispatch(setFcmToken(token)),
  159.   setStatusInbox: info => dispatch(setStatusInbox(info))
  160. });
  161.  
  162. export default connect(
  163.   mapStateToProps,
  164.   mapDispatchToProps
  165. )(SplashScreen);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top