Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Sample React Native App
- * https://github.com/facebook/react-native
- * @flow
- */
- import React, { Component } from 'react';
- import {
- Platform,
- StyleSheet,
- Text,
- View,
- TouchableOpacity
- } from 'react-native';
- import firebase from 'react-native-firebase';
- import type, { RemoteMessage, Notification, NotificationOpen } 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',
- });
- const notification = new firebase.notifications.Notification()
- .setNotificationId('notificationId')
- .setTitle('My notification title')
- .setBody('My notification body')
- .setData({
- key1: 'value1',
- key2: 'value2',
- });
- // // Build a channel
- // const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
- // .setDescription('My apps test channel');
- // // Create the channel
- // firebase.notifications().android.createChannel(channel);
- type Props = {};
- export default class App extends Component<Props> {
- componentDidMount() {
- firebase.messaging().hasPermission()
- .then(enabled => {
- if (enabled) {
- // user has permissions
- this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
- // Process your notification as required
- // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
- onNotification = (notification: Notification) => {
- notification.android.setChannelId(notification.data.channelId);
- firebase.notifications().displayNotification(notification);
- };
- });
- this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
- // Process your notification as required
- });
- this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
- // Get the action triggered by the notification being opened
- const action = notificationOpen.action;
- // Get information about the notification that was opened
- const notification: Notification = notificationOpen.notification;
- });
- firebase.notifications().getInitialNotification()
- .then((notificationOpen: NotificationOpen) => {
- if (notificationOpen) {
- // App was opened by a notification
- // Get the action triggered by the notification being opened
- const action = notificationOpen.action;
- // Get information about the notification that was opened
- const notification: Notification = notificationOpen.notification;
- }
- });
- } else {
- // user doesn't have permission
- firebase.messaging().requestPermission()
- .then(() => {
- // User has authorised
- this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
- // Process your notification as required
- // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
- });
- this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
- // Process your notification as required
- });
- })
- .catch(error => {
- // User has rejected permissions
- });
- }
- });
- }
- componentWillUnmount() {
- this.notificationDisplayedListener();
- this.notificationListener();
- this.notificationOpenedListener();
- }
- render() {
- return (
- <View style={styles.container}>
- <Text style={styles.welcome}>
- Welcome to React Native!
- </Text>
- <TouchableOpacity
- style={{backgroundColor: 'cyan'}}
- onPress={()=>{
- firebase.notifications().displayNotification(notification);
- // firebase.auth()
- // .signInAnonymouslyAndRetrieveData()
- // .then(credential => {
- // if (credential) {
- // console.log('default app user ->', credential.user.toJSON());
- // }
- // });
- }}>
- <Text>Test</Text>
- </TouchableOpacity>
- <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,
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment