Skip to content

Instantly share code, notes, and snippets.

@paulallies
Last active June 7, 2018 14:57
Show Gist options
  • Save paulallies/1b1892c20283260b6c75ccb2ff54358b to your computer and use it in GitHub Desktop.
Save paulallies/1b1892c20283260b6c75ccb2ff54358b to your computer and use it in GitHub Desktop.
updated App.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import firebase from 'react-native-firebase';
import { Notification } from 'react-native-firebase';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default class App extends Component {
componentDidMount() {
firebase.messaging().hasPermission()
.then(enabled => {
if (enabled) {
firebase.messaging().getToken().then(token => {
console.log("LOG: ", token);
})
// user has permissions
} else {
firebase.messaging().requestPermission()
.then(() => {
alert("User Now Has Permission")
})
.catch(error => {
console.log(error)
alert("Error", error)
// User has rejected permissions
});
}
});
this.notificationListener = firebase.notifications().onNotification((notification) => {
// Process your notification as required
const {
body,
data,
notificationId,
sound,
subtitle,
title
} = notification;
console.log("LOG: ", title, body, JSON.stringify(data))
});
}
componentWillUnmount() {
this.notificationListener();
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit App.js
</Text>
<Text style={styles.instructions}>
{instructions}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment