React Native - How to open route from push notification

前端 未结 3 940
花落未央
花落未央 2021-02-04 05:43

I\'m using react-navigation and react-native-push-notification. How can I open a certain StackNavigator\'s screen in onNotification<

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-04 06:32

    Seeing as how im using the legacy react-native-firebase I figured id post my solution for this issue, given that its slightly different than one of the above answers which utilizes RN-firebase V6. My solution is only slightly different, this solution works for notification handling with react-native-firebase v5.x :

    import * as React from 'react';
    import { Text, TextInput } from 'react-native';
    import AppNavigation from './src/navigation';
    import { Provider } from 'react-redux';
    import { store, persistor } from './src/store/index.js';
    import 'react-native-gesture-handler';
    import firebase from 'react-native-firebase';
    import { PersistGate } from 'redux-persist/integration/react';
    
    export default class App extends React.Component {
        constructor(props) {
            super(props);
            if (firebase.apps.length === 0) {
                firebase.initializeApp({});
            }
        }
    
        async componentDidMount() {
            // Initialize listener for when a notification has been displayed
            this.removeNotificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification) => {
                // process notification as required... android remote notif's do not have a "channel ID".
            });
    
            // Initialize listener for when a notification is received
            this.removeNotificationListener = firebase.notifications().onNotification((notification) => {
                // Process notification
            });
    
            // Listener for notification tap if in FOREGROUND & BACKGROUND
            this.removeNotificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
                // get the action trigger by the notification being opened
                const action = notificationOpen.action;
    
                // get info about the opened notification
                const info = notificationOpen.notification;
    
                // log for testing
                console.log('ACTION => ' + action + '\nNOTIFICATION INFO => ' + JSON.stringify(info));
            });
    
            // Listener for notification tap if app closed
            const notificationOpen = await firebase.notifications().getInitialNotification();
            if (notificationOpen) {
                // App was opened by notification
                const action = notificationOpen.action;
                const info = notificationOpen.notification;
    
                // log for testing:
                console.log('ACTION => ' + action + '\nNOTIFICATION INFO => ' + JSON.stringify(info));
            }
        }
    
        componentWillUnmount() {
            // Invoke these functions to un-subscribe the listener
            this.removeNotificationDisplayedListener();
            this.removeNotificationListener();
            this.removeNotificationOpenedListener();
        }
    
        render() {
            return (
                
                    
                        
                    
                
            );
        }
    }
    

提交回复
热议问题