Last active
March 28, 2019 11:40
-
-
Save guirald/85d8ce28f20faf1061c40e87d835c075 to your computer and use it in GitHub Desktop.
Loading.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/components/Loading.js | |
import React, { Component } from 'react' | |
import { View, Text, ActivityIndicator, StyleSheet, ImageBackground } from 'react-native' | |
import firebase from 'react-native-firebase' | |
import { AsyncStorage } from 'react-native'; | |
import { auth } from '../firebase' | |
export default class Loading extends Component { | |
async componentDidMount() { | |
auth.onAuthStateChanged(user => { | |
this.checkPermission() | |
this.props.navigation.navigate(user ? 'principal' : 'login') | |
}) | |
} | |
//1 | |
async checkPermission() { | |
const enabled = await firebase.messaging().hasPermission(); | |
if (enabled) { | |
this.getToken(); | |
} else { | |
this.requestPermission(); | |
} | |
} | |
//3 | |
async getToken() { | |
let fcmToken = await AsyncStorage.getItem('fcmToken', value); | |
if (!fcmToken) { | |
fcmToken = await firebase.messaging().getToken(); | |
if (fcmToken) { | |
// user has a device token | |
await AsyncStorage.setItem('fcmToken', fcmToken); | |
} | |
} | |
} | |
//2 | |
async requestPermission() { | |
try { | |
await firebase.messaging().requestPermission(); | |
// User has authorised | |
this.getToken(); | |
} catch (error) { | |
// User has rejected permissions | |
console.log('permission rejected'); | |
} | |
} | |
… | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment