Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { PureComponent } from "react";
- import {
- InteractionManager,
- StatusBar,
- Text,
- View,
- AsyncStorage
- } from "react-native";
- import firebase from "react-native-firebase";
- import { NavigationActions, StackActions } from "react-navigation";
- import { connect } from "react-redux";
- import { setFcmToken } from "../actions/auth";
- import globalStyles from "../assets/styles/global";
- import { setStatusInbox } from "../actions/notification";
- class SplashScreen extends PureComponent {
- static navigationOptions = {
- header: null
- };
- state = {
- ready: false
- };
- componentDidMount() {
- this.checkPermission();
- this.createNotificationListeners();
- InteractionManager.runAfterInteractions(() => {
- this.setState({ ready: true });
- if (this.props.isLogin) {
- return this.onResetNav("Home");
- } else {
- return this.onResetNav("Login");
- }
- });
- }
- async checkPermission() {
- const enabled = await firebase.messaging().hasPermission();
- if (enabled) {
- this.getToken();
- } else {
- this.requestPermission();
- }
- }
- async getToken() {
- let fcmToken = await AsyncStorage.getItem("fcmToken");
- if (!fcmToken) {
- fcmToken = await firebase.messaging().getToken();
- if (fcmToken) {
- await AsyncStorage.setItem("fcmToken", fcmToken);
- this.props.setFcmToken(fcmToken);
- }
- }
- this.props.setFcmToken(fcmToken);
- }
- async requestPermission() {
- try {
- await firebase.messaging().requestPermission();
- // User has authorised
- this.getToken();
- } catch (error) {
- // User has rejected permissions
- console.log("error", error);
- console.log("permission rejected");
- }
- }
- componentWillUnmount() {
- // this.messageListener();
- }
- async createNotificationListeners() {
- this.notificationListener = firebase
- .notifications()
- .onNotification(notifications => {
- const { title, body } = notifications;
- const notification = new firebase.notifications.Notification()
- .setNotificationId("notificationId")
- .setTitle(title)
- .setBody(body)
- .android.setChannelId("channelId")
- .android.setSmallIcon("ic_launcher")
- .setSound("default");
- firebase.notifications().displayNotification(notification);
- this.props.setStatusInbox(true);
- });
- this.notificationOpenedListener = firebase
- .notifications()
- .onNotificationOpened(notificationOpen => {
- const { title, body } = notificationOpen.notification;
- const id = 138;
- this.props.navigation.navigate("DetailOrder", {
- id,
- onNavigateBack: () => this.refresh(),
- status: "notif"
- });
- // this.props.navigation.push("Inbox");
- console.log("notif", this.props);
- });
- const notificationOpen = await firebase
- .notifications()
- .getInitialNotification();
- if (notificationOpen) {
- const { title, body } = notificationOpen.notification;
- }
- this.messageListener = firebase.messaging().onMessage(message => {
- console.log(JSON.stringify(message));
- });
- }
- onResetNav(routeName) {
- const resetAction = StackActions.reset({
- index: 0,
- actions: [NavigationActions.navigate({ routeName: routeName })]
- });
- this.props.navigation.dispatch(resetAction);
- }
- render() {
- if (!this.state.ready || this.state.ready == null) {
- return <View style={globalStyles.alignCenterContainer} />;
- }
- return (
- <View
- style={[globalStyles.centerContainer, { backgroundColor: "#2845b5" }]}
- >
- <StatusBar hidden />
- <Text
- style={[
- globalStyles.font,
- { fontWeight: "bold", fontSize: 20, color: "white" }
- ]}
- >
- Abunawas
- </Text>
- <Text style={[globalStyles.font, { color: "white" }]}>Corporate</Text>
- </View>
- );
- }
- }
- const mapStateToProps = ({ auth }) => ({
- isLogin: auth.isLogin
- });
- const mapDispatchToProps = dispatch => ({
- setFcmToken: token => dispatch(setFcmToken(token)),
- setStatusInbox: info => dispatch(setStatusInbox(info))
- });
- export default connect(
- mapStateToProps,
- mapDispatchToProps
- )(SplashScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement