Background Notification when something changes with an app React Native

Viewed 1051

I am new to react native and am trying to create an app that I would like to received background notifications when some new content as been added.

I have set up background notifications using firebase cloud messaging, however I have noticed that this only allows me to send messages to devices and takes the user to the home screen. I would like the when the user receives the notification and they click on it that it would take them to a certain screen depending on the notification.

After doing some research I can't seem to find any way to do this, would anyone know how this can be done or know of a tutorial for this?

1 Answers

Are you using react-native-firebase lib? If not I hight recommend it. The API provides two APIs to handle notification interactions.

  • getInitialNotification: When the application is opened from a quit state.
  • onNotificationOpenedApp: When the application is running, but in the background.

  useEffect(() => {
    // Assume a message-notification contains a "type" property in the data payload of the screen to open

    messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate(remoteMessage.data.type);
    });

    // Check whether an initial notification is available
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
        }
        setLoading(false);
      });
  }, []);

Please, check the documentation at the Notification Handling Interaction Section

Related