Search code examples
react-nativepush-notificationdeep-linking

React Native: Opening a push notification at a specific deeplink URL


I'm trying to open an app at a specific screen, from a push notification. My understanding is that I use a "deep link" to do this. I've set up react-navigation to handle the deep link (and AndroidManifest.xml & appDelegate.m), and can open the deep link with xcrun simctl openurl booted myapp://a/1 / adb shell am start -W -a android.intent.action.VIEW -d "myapp://a/1" app.bundle.name.

But I can't get the push notification to open the app, at the deep link, when the app is closed; it only opens the app on the home screen (on both iOS and Android). How do I get the app to open at the deep link (or alternatively, to read the contents of the push notification so that I can navigate to the correct screen myself)?

library versions:

  • react-native 0.62.2
  • @react-navigation 5.5.1
  • react-native-fcm 16.2.4

Solution

  • After some debugging, I found it was actually a problem in my code handling notifications. I was checking for an ID property in the notification body, but not all notifications have one. So once I removed this, it started working. So to make it work you should have something like:

    // push handler component
    class PushHandler extends React.Component {
      componentDidMount() {
        FCM.getInitialNotification(notification => {
          if (!notification) {
            // the app was not opened with a notification.
            return;
          }
    
          // extract route from `notification` here
    
          this.props.navigation.navigate(route);
        }
      }
    
      // ... rest of push notification handling code
    }
    

    It also didn't have anything to do with deep linking. The this.props.navigation.navigate is just the plain in-app navigation.