Devakanta Rao
Devakanta Rao

Reputation: 105

Navigate to specific screen when notification is clicked - React Native

I am using the react-native-firebase library for FCM capabilities in my React Native app. I have followed the initial guide provided on their website https://rnfirebase.io/docs/v4.2.x/installation/initial-setup. I have also set up the Cloud Messaging and Notifications module for Android. The notifications are arriving at device correctly. But, the problem is I am not able to route to a particular screen when the notification is clicked. I am using react-navigation library for navigation. I am actually very new to React native so I don't know how to write code for this functionality. Any help is appreciated. The main thing is I am confused where should I write code for navigation. Also, I want it to work even when the app is closed and the notification is clicked.

Upvotes: 1

Views: 5909

Answers (1)

Nikolai
Nikolai

Reputation: 347

How to set up deep linking in react-navigation: https://reactnavigation.org/docs/en/deep-linking.html

How to set up notifications to trigger deep links:

handleNotification (notification) {
    const { data } = notification;
    const prefix = Platform.OS === 'android' 
                     ? 'myapp://myapp/'
                     : 'myapp://'
    const url = `${prefix}${data.path}/${data.id}`;

    if (notification.data.path) {
        Linking.openURL(url).catch(err => console.error(err));
        firebase.messaging().setBadgeNumber(0);
    }
  }

Upvotes: 6

Related Questions