3

I want to open a particular page if the notification is clicked. How to do it in React Native?

this.notificationListener = firebase.notifications().onNotification((notification: Notification) => { 
  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. 

   const { title, body } = notification; }
  );

 const { title, body } = notification; 
});

1 Answer 1

7
async setupNotification() {

    firebase.notifications().getInitialNotification()
        .then((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 = notificationOpen.notification;
            }
        });

    const channel = new firebase.notifications.Android.Channel(
        'channelId',
        'ChannelName',
        firebase.notifications.Android.Importance.Max
    ).setDescription('A natural description of the channel');
    firebase.notifications().android.createChannel(channel);

    // the listener returns a function you can use to unsubscribe
    this.unsubscribeFromNotificationListener = firebase.notifications().onNotification((notification) => {
        console.log('local notifciation ' + JSON.stringify(notification.data))
        try {
            if (Platform.OS === 'ios') {
                const localNotification = new firebase.notifications.Notification()
                    .setNotificationId(notification.notificationId)
                    .setTitle(notification.title)
                    .setSound('default')
                    .setSubtitle(notification.subtitle)
                    .setBody(notification.body)
                    .setData(notification.data)
                    .ios.setBadge(notification.ios.badge);
                firebase.notifications()
                    .displayNotification(localNotification)
                    .catch(err => console.error(err));
                // alert('Local notification')
            }
        } catch (error) {
            alert(error)
        }

    });


    const notificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
        // alert('Remote notification from killed state to foreground state')
        // App was opened by a notification
        // Get the action triggered by the notification being opened from killed state to foreground
        const action = notificationOpen.action;
        // Get information about the notification that was opened
        const notification = notificationOpen.notification;
        if (notification.data) { // && Platform.OS !== 'ios'
            setTimeout(() => {
                this.handleNavigation(notification.data);

            }, 100);
        }


        firebase.notifications().removeAllDeliveredNotifications();

    }

    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
        // Get the action triggered by the notification being opened from background state to foreground
        // alert('Remote notification from background state to foreground state')
        const action = notificationOpen.action;
        // Get information about the notification that was opened
        const notification = notificationOpen.notification;

        if (notification.data) { // && Platform.OS !== 'ios'
            setTimeout(() => {

                    this.handleNavigation(notification.data);
                }


            }, 100);
        }
        //Remove delivered notifications from notification tray
        firebase.notifications().removeAllDeliveredNotifications();
    });


}

=========================
Handle Navigation Method
=========================

    handleNavigation(notif) {

    let type = notif.type ? notif.type : ''

    if (type === 'library') {
       this.props.navigation.navigate('ScreenName')
    }

}
==== Called Setup Notification Method ====
constructor() {
    super();
    this.state = {};
    this.setupNotification()
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why do you set the getInitialNotification listener 2 times? Is the sequencie important? Could you please provide more information about your solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.