Advertisement
dobrado

RootNavigation.js

May 29th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Notifications } from 'expo';
  2. import React from 'react';
  3. import { StackNavigator } from 'react-navigation';
  4. import { TouchableOpacity } from 'react-native';
  5. import { Entypo } from '@expo/vector-icons';
  6. import MainTabNavigator from './MainTabNavigator';
  7. import registerForPushNotificationsAsync from '../api/registerForPushNotificationsAsync';
  8. import HomeScreen from '../screens/HomeScreen';
  9.  
  10. const RootStackNavigator = StackNavigator(
  11.   {
  12.     HomeScreen: {
  13.       screen: HomeScreen,
  14.       navigationOptions: {
  15.         tabBarVisible: false
  16.       }
  17.     },
  18.     Main: {
  19.       screen: MainTabNavigator,
  20.     }
  21. // other routes objects
  22.   },
  23.   {
  24.     // initialRoute: 'HomeScreen',
  25.     navigationOptions: ({ navigation }) => ({
  26.       headerTitleStyle: {
  27.         fontWeight: 'normal',
  28.       },
  29.       headerLeft:
  30.       <TouchableOpacity
  31.         onPress={() => {
  32.           if(navigation.state.index === 0) {
  33.             navigation.navigate('DrawerOpen');
  34.           } else {
  35.             navigation.navigate('DrawerClose');
  36.           }
  37.         }}
  38.                style={{ padding: 10 }}>
  39.           <Entypo name={navigation.state.index === 0 ? 'menu' : 'chevron-small-left'} size={32} color="#000"         >
  40.       </TouchableOpacity>
  41.     }),
  42.   }
  43. );
  44.  
  45. export default class RootNavigator extends React.Component {
  46.   componentDidMount() {
  47.     this._notificationSubscription = this._registerForPushNotifications();
  48.   }
  49.  
  50.   componentWillUnmount() {
  51.     this._notificationSubscription && this._notificationSubscription.remove();
  52.   }
  53.  
  54.   render() {
  55.     return <RootStackNavigator />;
  56.   }
  57.  
  58.   _registerForPushNotifications() {
  59.     // Send our push token over to our backend so we can receive notifications
  60.     // You can comment the following line out if you want to stop receiving
  61.     // a notification every time you open the app. Check out the source
  62.     // for this function in api/registerForPushNotificationsAsync.js
  63.     registerForPushNotificationsAsync();
  64.  
  65.     // Watch for incoming notifications
  66.     this._notificationSubscription = Notifications.addListener(this._handleNotification);
  67.   }
  68.  
  69.   _handleNotification = ({ origin, data }) => {
  70.     console.log(`Push notification ${origin} with data: ${JSON.stringify(data)}`);
  71.   };
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement