Deep links in react-native-firebase notifications











up vote
0
down vote

favorite












I am using react-native-firebase with messaging to deliver notifications to my app with cloud functions, with admin.messaging().send(message), very similar to here: https://medium.com/the-modern-development-stack/react-native-push-notifications-with-firebase-cloud-functions-74b832d45386 .



I receive notifications when the app is in the background. Right now I am sending a text in the body of the notification, like 'a new location has been added to the map'. I want to be able to add some sort of deep link, so that when I swipe View on the notification (on iOS for example), it will take me to a specific screen inside the app. How do I pass data from the notification to the app?



I am using react-native-navigation in the app. I can only find code about deep links from inside the app (https://wix.github.io/react-native-navigation/#/deep-links?id=deep-links).










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am using react-native-firebase with messaging to deliver notifications to my app with cloud functions, with admin.messaging().send(message), very similar to here: https://medium.com/the-modern-development-stack/react-native-push-notifications-with-firebase-cloud-functions-74b832d45386 .



    I receive notifications when the app is in the background. Right now I am sending a text in the body of the notification, like 'a new location has been added to the map'. I want to be able to add some sort of deep link, so that when I swipe View on the notification (on iOS for example), it will take me to a specific screen inside the app. How do I pass data from the notification to the app?



    I am using react-native-navigation in the app. I can only find code about deep links from inside the app (https://wix.github.io/react-native-navigation/#/deep-links?id=deep-links).










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am using react-native-firebase with messaging to deliver notifications to my app with cloud functions, with admin.messaging().send(message), very similar to here: https://medium.com/the-modern-development-stack/react-native-push-notifications-with-firebase-cloud-functions-74b832d45386 .



      I receive notifications when the app is in the background. Right now I am sending a text in the body of the notification, like 'a new location has been added to the map'. I want to be able to add some sort of deep link, so that when I swipe View on the notification (on iOS for example), it will take me to a specific screen inside the app. How do I pass data from the notification to the app?



      I am using react-native-navigation in the app. I can only find code about deep links from inside the app (https://wix.github.io/react-native-navigation/#/deep-links?id=deep-links).










      share|improve this question













      I am using react-native-firebase with messaging to deliver notifications to my app with cloud functions, with admin.messaging().send(message), very similar to here: https://medium.com/the-modern-development-stack/react-native-push-notifications-with-firebase-cloud-functions-74b832d45386 .



      I receive notifications when the app is in the background. Right now I am sending a text in the body of the notification, like 'a new location has been added to the map'. I want to be able to add some sort of deep link, so that when I swipe View on the notification (on iOS for example), it will take me to a specific screen inside the app. How do I pass data from the notification to the app?



      I am using react-native-navigation in the app. I can only find code about deep links from inside the app (https://wix.github.io/react-native-navigation/#/deep-links?id=deep-links).







      react-native react-native-navigation react-native-firebase






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 3 at 12:53









      Nicoara Talpes

      189116




      189116
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          I think you don't need to use deep links nor dynamic links but just use Firebase/Notifications properly. If I were you I would add the following logic in the componentDidMount method of your parent container:



          async componentDidMount() {

          // 1. Check notification permission
          const notificationsEnabled = await firebase.messaging().hasPermission();
          if (!notificationsEnabled) {
          try {
          await firebase.messaging().requestPermission(); // Request notification permission
          // At this point the user has authorized the notifications
          } catch (error) {
          // The user has NOT authorized the notifications
          }
          }

          // 2. Get the registration token for firebase notifications
          const fcmToken = await firebase.messaging().getToken();
          // Save the token

          // 3. Listen for notifications. To do that, react-native-firebase offer you some methods:
          firebase.messaging().onMessage(message => { /* */ })

          firebase.notifications().onNotificationDisplayed(notification => { /* */ })

          firebase.messaging().onNotification(notification => { /* */ })

          firebase.messaging().onNotificationOpened(notification => {
          /* For instance, you could use it and do the NAVIGATION at this point
          this.props.navigation.navigate('SomeScreen');
          // Note that you can send whatever you want in the *notification* object, so you can add to the notification the route name of the screen you want to navigate to.
          */
          })

          }


          You can find the documentation here: https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications






          share|improve this answer




























            up vote
            1
            down vote













            I think you are fine with the "how firebase notification work"... cause of this, here is only an description of the Logic how you can Deeplinking into your App.



            If you send a notification, add a data-field. Let's say your app has a Tab-Navigator and the sections "News","Service" and "Review".
            In your Push-Notification - Datafield (let's name it "jumpToScreen" you define your value:




            jumpToScreen = Service




            I assume you still have the Handling to recieve Notifications from Firebase implemented.
            So create an /lib/MessageHandler.js Class and put your business-logic inside.



            import firebase from 'react-native-firebase';

            /*
            * Get a string from Firebase-Messages and return the Screen to jump to
            */
            const getJumpPoint = (pointer) => {
            switch (pointer) {
            case 'News':
            return 'NAV_NewsList'; // <= this are the names of your Screens
            case 'Service':
            return 'NAV_ServiceList';
            case 'Review':
            return 'NAV_ReviewDetail';
            default: return false;
            }
            };

            const MessageHandler = {
            /**
            * initPushNotification initialize Firebase Messaging
            * @return fcmToken String
            */
            initPushNotification: async () => {
            try {
            const notificationPermission = await firebase.messaging().hasPermission();
            MessageHandler.setNotificationChannels();

            if (notificationPermission) {
            try {
            return await MessageHandler.getNotificationToken();
            } catch (error) {
            console.log(`Error: failed to get Notification-Token n ${error}`);
            }
            }
            } catch (error) {
            console.log(`Error while checking Notification-Permissionn ${error}`);
            }
            return false;
            },
            clearBadges: () => {
            firebase.notifications().setBadge(0);
            },
            getNotificationToken: () => firebase.messaging().getToken(),
            setNotificationChannels() {
            try {
            /* Notification-Channels is a must-have for Android >= 8 */
            const channel = new firebase.notifications.Android.Channel(
            'app-infos',
            'App Infos',
            firebase.notifications.Android.Importance.Max,
            ).setDescription('General Information');

            firebase.notifications().android.createChannel(channel);
            } catch (error) {
            console.log('Error while creating Push_Notification-Channel');
            }
            },
            requestPermission: () => {
            try {
            firebase.messaging().requestPermission();
            firebase.analytics().logEvent('pushNotification_permission', { decision: 'denied' });
            } catch (error) {
            // User has rejected permissions
            firebase.analytics().logEvent('pushNotification_permission', { decision: 'allowed' });
            }
            },
            foregroundNotificationListener: (navigation) => {
            // In-App Messages if App in Foreground
            firebase.notifications().onNotification((notification) => {
            MessageHandler.setNotificationChannels();
            navigation.navigate(getJumpPoint(notification.data.screen));
            });
            },
            backgroundNotificationListener: (navigation) => {
            // In-App Messages if App in Background
            firebase.notifications().onNotificationOpened((notificationOpen) => {
            const { notification } = notificationOpen;
            notification.android.setChannelId('app-infos');
            if (notification.data.screen !== undefined) {
            navigation.navigate(getJumpPoint(notification.data.screen));
            }
            });
            },
            appInitNotificationListener: () => {
            // In-App Messages if App in Background
            firebase.notifications().onNotificationOpend((notification) => {
            notification.android.setChannelId('app-infos');
            console.log('App-Init: Da kommt ne Message rein', notification);
            firebase.notifications().displayNotification(notification);
            });
            },
            };

            export default MessageHandler;


            In your index.js you can connect it like this:



            import MessageHandler from './lib/MessageHandler';
            export default class App extends Component {
            state = {
            loading: null,
            connection: null,
            settings: null,
            };
            async componentDidMount() {
            const { navigation } = this.props;
            await MessageHandler.initPushNotification();
            this.notificationForegroundListener = MessageHandler.foregroundNotificationListener(navigation);
            this.notificationBackgroundListener = MessageHandler.backgroundNotificationListener(navigation);

            this.setState({ loading: false, data });
            }

            componentWillUnmount() {
            this.notificationForegroundListener();
            this.notificationBackgroundListener();
            }
            async componentDidMount() {
            MessageHandler.requestPermission();
            AppState.addEventListener('change', this.handleAppStateChange);
            MessageHandler.clearBadges();
            }

            componentWillUnmount() {
            AppState.removeEventListener('change', this.handleAppStateChange);
            }
            handleAppStateChange = (nextAppState) => {
            if (nextAppState.match(/inactive|background/)) {
            MessageHandler.clearBadges();
            }
            ....


            I hope this give you an Idea how to implement it for your needs.






            share|improve this answer




























              up vote
              0
              down vote



              accepted










              My solution was to use add what information I need in the data object of the notification message object:



              in functions/index.js:



                let message = {
              notification: {
              body: `new notification `
              },
              token: pushToken,
              data: {
              type: 'NEW_TRAINING',
              title: locationTitle
              }
              };


              and process as follows in the app for navigation:



              this.notificationOpenedListener =
              firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
              if (notification.data.type === 'NEW_TRAINING') {
              this.props.navigator.push({
              screen: 'newtrainingscreen',
              title: notification.data.title,
              animated: true
              });
              }





              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52150199%2fdeep-links-in-react-native-firebase-notifications%23new-answer', 'question_page');
                }
                );

                Post as a guest
































                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                1
                down vote













                I think you don't need to use deep links nor dynamic links but just use Firebase/Notifications properly. If I were you I would add the following logic in the componentDidMount method of your parent container:



                async componentDidMount() {

                // 1. Check notification permission
                const notificationsEnabled = await firebase.messaging().hasPermission();
                if (!notificationsEnabled) {
                try {
                await firebase.messaging().requestPermission(); // Request notification permission
                // At this point the user has authorized the notifications
                } catch (error) {
                // The user has NOT authorized the notifications
                }
                }

                // 2. Get the registration token for firebase notifications
                const fcmToken = await firebase.messaging().getToken();
                // Save the token

                // 3. Listen for notifications. To do that, react-native-firebase offer you some methods:
                firebase.messaging().onMessage(message => { /* */ })

                firebase.notifications().onNotificationDisplayed(notification => { /* */ })

                firebase.messaging().onNotification(notification => { /* */ })

                firebase.messaging().onNotificationOpened(notification => {
                /* For instance, you could use it and do the NAVIGATION at this point
                this.props.navigation.navigate('SomeScreen');
                // Note that you can send whatever you want in the *notification* object, so you can add to the notification the route name of the screen you want to navigate to.
                */
                })

                }


                You can find the documentation here: https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications






                share|improve this answer

























                  up vote
                  1
                  down vote













                  I think you don't need to use deep links nor dynamic links but just use Firebase/Notifications properly. If I were you I would add the following logic in the componentDidMount method of your parent container:



                  async componentDidMount() {

                  // 1. Check notification permission
                  const notificationsEnabled = await firebase.messaging().hasPermission();
                  if (!notificationsEnabled) {
                  try {
                  await firebase.messaging().requestPermission(); // Request notification permission
                  // At this point the user has authorized the notifications
                  } catch (error) {
                  // The user has NOT authorized the notifications
                  }
                  }

                  // 2. Get the registration token for firebase notifications
                  const fcmToken = await firebase.messaging().getToken();
                  // Save the token

                  // 3. Listen for notifications. To do that, react-native-firebase offer you some methods:
                  firebase.messaging().onMessage(message => { /* */ })

                  firebase.notifications().onNotificationDisplayed(notification => { /* */ })

                  firebase.messaging().onNotification(notification => { /* */ })

                  firebase.messaging().onNotificationOpened(notification => {
                  /* For instance, you could use it and do the NAVIGATION at this point
                  this.props.navigation.navigate('SomeScreen');
                  // Note that you can send whatever you want in the *notification* object, so you can add to the notification the route name of the screen you want to navigate to.
                  */
                  })

                  }


                  You can find the documentation here: https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications






                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    I think you don't need to use deep links nor dynamic links but just use Firebase/Notifications properly. If I were you I would add the following logic in the componentDidMount method of your parent container:



                    async componentDidMount() {

                    // 1. Check notification permission
                    const notificationsEnabled = await firebase.messaging().hasPermission();
                    if (!notificationsEnabled) {
                    try {
                    await firebase.messaging().requestPermission(); // Request notification permission
                    // At this point the user has authorized the notifications
                    } catch (error) {
                    // The user has NOT authorized the notifications
                    }
                    }

                    // 2. Get the registration token for firebase notifications
                    const fcmToken = await firebase.messaging().getToken();
                    // Save the token

                    // 3. Listen for notifications. To do that, react-native-firebase offer you some methods:
                    firebase.messaging().onMessage(message => { /* */ })

                    firebase.notifications().onNotificationDisplayed(notification => { /* */ })

                    firebase.messaging().onNotification(notification => { /* */ })

                    firebase.messaging().onNotificationOpened(notification => {
                    /* For instance, you could use it and do the NAVIGATION at this point
                    this.props.navigation.navigate('SomeScreen');
                    // Note that you can send whatever you want in the *notification* object, so you can add to the notification the route name of the screen you want to navigate to.
                    */
                    })

                    }


                    You can find the documentation here: https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications






                    share|improve this answer












                    I think you don't need to use deep links nor dynamic links but just use Firebase/Notifications properly. If I were you I would add the following logic in the componentDidMount method of your parent container:



                    async componentDidMount() {

                    // 1. Check notification permission
                    const notificationsEnabled = await firebase.messaging().hasPermission();
                    if (!notificationsEnabled) {
                    try {
                    await firebase.messaging().requestPermission(); // Request notification permission
                    // At this point the user has authorized the notifications
                    } catch (error) {
                    // The user has NOT authorized the notifications
                    }
                    }

                    // 2. Get the registration token for firebase notifications
                    const fcmToken = await firebase.messaging().getToken();
                    // Save the token

                    // 3. Listen for notifications. To do that, react-native-firebase offer you some methods:
                    firebase.messaging().onMessage(message => { /* */ })

                    firebase.notifications().onNotificationDisplayed(notification => { /* */ })

                    firebase.messaging().onNotification(notification => { /* */ })

                    firebase.messaging().onNotificationOpened(notification => {
                    /* For instance, you could use it and do the NAVIGATION at this point
                    this.props.navigation.navigate('SomeScreen');
                    // Note that you can send whatever you want in the *notification* object, so you can add to the notification the route name of the screen you want to navigate to.
                    */
                    })

                    }


                    You can find the documentation here: https://rnfirebase.io/docs/v4.3.x/notifications/receiving-notifications







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 4 at 22:34









                    Nacho Justicia Ramos

                    10927




                    10927
























                        up vote
                        1
                        down vote













                        I think you are fine with the "how firebase notification work"... cause of this, here is only an description of the Logic how you can Deeplinking into your App.



                        If you send a notification, add a data-field. Let's say your app has a Tab-Navigator and the sections "News","Service" and "Review".
                        In your Push-Notification - Datafield (let's name it "jumpToScreen" you define your value:




                        jumpToScreen = Service




                        I assume you still have the Handling to recieve Notifications from Firebase implemented.
                        So create an /lib/MessageHandler.js Class and put your business-logic inside.



                        import firebase from 'react-native-firebase';

                        /*
                        * Get a string from Firebase-Messages and return the Screen to jump to
                        */
                        const getJumpPoint = (pointer) => {
                        switch (pointer) {
                        case 'News':
                        return 'NAV_NewsList'; // <= this are the names of your Screens
                        case 'Service':
                        return 'NAV_ServiceList';
                        case 'Review':
                        return 'NAV_ReviewDetail';
                        default: return false;
                        }
                        };

                        const MessageHandler = {
                        /**
                        * initPushNotification initialize Firebase Messaging
                        * @return fcmToken String
                        */
                        initPushNotification: async () => {
                        try {
                        const notificationPermission = await firebase.messaging().hasPermission();
                        MessageHandler.setNotificationChannels();

                        if (notificationPermission) {
                        try {
                        return await MessageHandler.getNotificationToken();
                        } catch (error) {
                        console.log(`Error: failed to get Notification-Token n ${error}`);
                        }
                        }
                        } catch (error) {
                        console.log(`Error while checking Notification-Permissionn ${error}`);
                        }
                        return false;
                        },
                        clearBadges: () => {
                        firebase.notifications().setBadge(0);
                        },
                        getNotificationToken: () => firebase.messaging().getToken(),
                        setNotificationChannels() {
                        try {
                        /* Notification-Channels is a must-have for Android >= 8 */
                        const channel = new firebase.notifications.Android.Channel(
                        'app-infos',
                        'App Infos',
                        firebase.notifications.Android.Importance.Max,
                        ).setDescription('General Information');

                        firebase.notifications().android.createChannel(channel);
                        } catch (error) {
                        console.log('Error while creating Push_Notification-Channel');
                        }
                        },
                        requestPermission: () => {
                        try {
                        firebase.messaging().requestPermission();
                        firebase.analytics().logEvent('pushNotification_permission', { decision: 'denied' });
                        } catch (error) {
                        // User has rejected permissions
                        firebase.analytics().logEvent('pushNotification_permission', { decision: 'allowed' });
                        }
                        },
                        foregroundNotificationListener: (navigation) => {
                        // In-App Messages if App in Foreground
                        firebase.notifications().onNotification((notification) => {
                        MessageHandler.setNotificationChannels();
                        navigation.navigate(getJumpPoint(notification.data.screen));
                        });
                        },
                        backgroundNotificationListener: (navigation) => {
                        // In-App Messages if App in Background
                        firebase.notifications().onNotificationOpened((notificationOpen) => {
                        const { notification } = notificationOpen;
                        notification.android.setChannelId('app-infos');
                        if (notification.data.screen !== undefined) {
                        navigation.navigate(getJumpPoint(notification.data.screen));
                        }
                        });
                        },
                        appInitNotificationListener: () => {
                        // In-App Messages if App in Background
                        firebase.notifications().onNotificationOpend((notification) => {
                        notification.android.setChannelId('app-infos');
                        console.log('App-Init: Da kommt ne Message rein', notification);
                        firebase.notifications().displayNotification(notification);
                        });
                        },
                        };

                        export default MessageHandler;


                        In your index.js you can connect it like this:



                        import MessageHandler from './lib/MessageHandler';
                        export default class App extends Component {
                        state = {
                        loading: null,
                        connection: null,
                        settings: null,
                        };
                        async componentDidMount() {
                        const { navigation } = this.props;
                        await MessageHandler.initPushNotification();
                        this.notificationForegroundListener = MessageHandler.foregroundNotificationListener(navigation);
                        this.notificationBackgroundListener = MessageHandler.backgroundNotificationListener(navigation);

                        this.setState({ loading: false, data });
                        }

                        componentWillUnmount() {
                        this.notificationForegroundListener();
                        this.notificationBackgroundListener();
                        }
                        async componentDidMount() {
                        MessageHandler.requestPermission();
                        AppState.addEventListener('change', this.handleAppStateChange);
                        MessageHandler.clearBadges();
                        }

                        componentWillUnmount() {
                        AppState.removeEventListener('change', this.handleAppStateChange);
                        }
                        handleAppStateChange = (nextAppState) => {
                        if (nextAppState.match(/inactive|background/)) {
                        MessageHandler.clearBadges();
                        }
                        ....


                        I hope this give you an Idea how to implement it for your needs.






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          I think you are fine with the "how firebase notification work"... cause of this, here is only an description of the Logic how you can Deeplinking into your App.



                          If you send a notification, add a data-field. Let's say your app has a Tab-Navigator and the sections "News","Service" and "Review".
                          In your Push-Notification - Datafield (let's name it "jumpToScreen" you define your value:




                          jumpToScreen = Service




                          I assume you still have the Handling to recieve Notifications from Firebase implemented.
                          So create an /lib/MessageHandler.js Class and put your business-logic inside.



                          import firebase from 'react-native-firebase';

                          /*
                          * Get a string from Firebase-Messages and return the Screen to jump to
                          */
                          const getJumpPoint = (pointer) => {
                          switch (pointer) {
                          case 'News':
                          return 'NAV_NewsList'; // <= this are the names of your Screens
                          case 'Service':
                          return 'NAV_ServiceList';
                          case 'Review':
                          return 'NAV_ReviewDetail';
                          default: return false;
                          }
                          };

                          const MessageHandler = {
                          /**
                          * initPushNotification initialize Firebase Messaging
                          * @return fcmToken String
                          */
                          initPushNotification: async () => {
                          try {
                          const notificationPermission = await firebase.messaging().hasPermission();
                          MessageHandler.setNotificationChannels();

                          if (notificationPermission) {
                          try {
                          return await MessageHandler.getNotificationToken();
                          } catch (error) {
                          console.log(`Error: failed to get Notification-Token n ${error}`);
                          }
                          }
                          } catch (error) {
                          console.log(`Error while checking Notification-Permissionn ${error}`);
                          }
                          return false;
                          },
                          clearBadges: () => {
                          firebase.notifications().setBadge(0);
                          },
                          getNotificationToken: () => firebase.messaging().getToken(),
                          setNotificationChannels() {
                          try {
                          /* Notification-Channels is a must-have for Android >= 8 */
                          const channel = new firebase.notifications.Android.Channel(
                          'app-infos',
                          'App Infos',
                          firebase.notifications.Android.Importance.Max,
                          ).setDescription('General Information');

                          firebase.notifications().android.createChannel(channel);
                          } catch (error) {
                          console.log('Error while creating Push_Notification-Channel');
                          }
                          },
                          requestPermission: () => {
                          try {
                          firebase.messaging().requestPermission();
                          firebase.analytics().logEvent('pushNotification_permission', { decision: 'denied' });
                          } catch (error) {
                          // User has rejected permissions
                          firebase.analytics().logEvent('pushNotification_permission', { decision: 'allowed' });
                          }
                          },
                          foregroundNotificationListener: (navigation) => {
                          // In-App Messages if App in Foreground
                          firebase.notifications().onNotification((notification) => {
                          MessageHandler.setNotificationChannels();
                          navigation.navigate(getJumpPoint(notification.data.screen));
                          });
                          },
                          backgroundNotificationListener: (navigation) => {
                          // In-App Messages if App in Background
                          firebase.notifications().onNotificationOpened((notificationOpen) => {
                          const { notification } = notificationOpen;
                          notification.android.setChannelId('app-infos');
                          if (notification.data.screen !== undefined) {
                          navigation.navigate(getJumpPoint(notification.data.screen));
                          }
                          });
                          },
                          appInitNotificationListener: () => {
                          // In-App Messages if App in Background
                          firebase.notifications().onNotificationOpend((notification) => {
                          notification.android.setChannelId('app-infos');
                          console.log('App-Init: Da kommt ne Message rein', notification);
                          firebase.notifications().displayNotification(notification);
                          });
                          },
                          };

                          export default MessageHandler;


                          In your index.js you can connect it like this:



                          import MessageHandler from './lib/MessageHandler';
                          export default class App extends Component {
                          state = {
                          loading: null,
                          connection: null,
                          settings: null,
                          };
                          async componentDidMount() {
                          const { navigation } = this.props;
                          await MessageHandler.initPushNotification();
                          this.notificationForegroundListener = MessageHandler.foregroundNotificationListener(navigation);
                          this.notificationBackgroundListener = MessageHandler.backgroundNotificationListener(navigation);

                          this.setState({ loading: false, data });
                          }

                          componentWillUnmount() {
                          this.notificationForegroundListener();
                          this.notificationBackgroundListener();
                          }
                          async componentDidMount() {
                          MessageHandler.requestPermission();
                          AppState.addEventListener('change', this.handleAppStateChange);
                          MessageHandler.clearBadges();
                          }

                          componentWillUnmount() {
                          AppState.removeEventListener('change', this.handleAppStateChange);
                          }
                          handleAppStateChange = (nextAppState) => {
                          if (nextAppState.match(/inactive|background/)) {
                          MessageHandler.clearBadges();
                          }
                          ....


                          I hope this give you an Idea how to implement it for your needs.






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            I think you are fine with the "how firebase notification work"... cause of this, here is only an description of the Logic how you can Deeplinking into your App.



                            If you send a notification, add a data-field. Let's say your app has a Tab-Navigator and the sections "News","Service" and "Review".
                            In your Push-Notification - Datafield (let's name it "jumpToScreen" you define your value:




                            jumpToScreen = Service




                            I assume you still have the Handling to recieve Notifications from Firebase implemented.
                            So create an /lib/MessageHandler.js Class and put your business-logic inside.



                            import firebase from 'react-native-firebase';

                            /*
                            * Get a string from Firebase-Messages and return the Screen to jump to
                            */
                            const getJumpPoint = (pointer) => {
                            switch (pointer) {
                            case 'News':
                            return 'NAV_NewsList'; // <= this are the names of your Screens
                            case 'Service':
                            return 'NAV_ServiceList';
                            case 'Review':
                            return 'NAV_ReviewDetail';
                            default: return false;
                            }
                            };

                            const MessageHandler = {
                            /**
                            * initPushNotification initialize Firebase Messaging
                            * @return fcmToken String
                            */
                            initPushNotification: async () => {
                            try {
                            const notificationPermission = await firebase.messaging().hasPermission();
                            MessageHandler.setNotificationChannels();

                            if (notificationPermission) {
                            try {
                            return await MessageHandler.getNotificationToken();
                            } catch (error) {
                            console.log(`Error: failed to get Notification-Token n ${error}`);
                            }
                            }
                            } catch (error) {
                            console.log(`Error while checking Notification-Permissionn ${error}`);
                            }
                            return false;
                            },
                            clearBadges: () => {
                            firebase.notifications().setBadge(0);
                            },
                            getNotificationToken: () => firebase.messaging().getToken(),
                            setNotificationChannels() {
                            try {
                            /* Notification-Channels is a must-have for Android >= 8 */
                            const channel = new firebase.notifications.Android.Channel(
                            'app-infos',
                            'App Infos',
                            firebase.notifications.Android.Importance.Max,
                            ).setDescription('General Information');

                            firebase.notifications().android.createChannel(channel);
                            } catch (error) {
                            console.log('Error while creating Push_Notification-Channel');
                            }
                            },
                            requestPermission: () => {
                            try {
                            firebase.messaging().requestPermission();
                            firebase.analytics().logEvent('pushNotification_permission', { decision: 'denied' });
                            } catch (error) {
                            // User has rejected permissions
                            firebase.analytics().logEvent('pushNotification_permission', { decision: 'allowed' });
                            }
                            },
                            foregroundNotificationListener: (navigation) => {
                            // In-App Messages if App in Foreground
                            firebase.notifications().onNotification((notification) => {
                            MessageHandler.setNotificationChannels();
                            navigation.navigate(getJumpPoint(notification.data.screen));
                            });
                            },
                            backgroundNotificationListener: (navigation) => {
                            // In-App Messages if App in Background
                            firebase.notifications().onNotificationOpened((notificationOpen) => {
                            const { notification } = notificationOpen;
                            notification.android.setChannelId('app-infos');
                            if (notification.data.screen !== undefined) {
                            navigation.navigate(getJumpPoint(notification.data.screen));
                            }
                            });
                            },
                            appInitNotificationListener: () => {
                            // In-App Messages if App in Background
                            firebase.notifications().onNotificationOpend((notification) => {
                            notification.android.setChannelId('app-infos');
                            console.log('App-Init: Da kommt ne Message rein', notification);
                            firebase.notifications().displayNotification(notification);
                            });
                            },
                            };

                            export default MessageHandler;


                            In your index.js you can connect it like this:



                            import MessageHandler from './lib/MessageHandler';
                            export default class App extends Component {
                            state = {
                            loading: null,
                            connection: null,
                            settings: null,
                            };
                            async componentDidMount() {
                            const { navigation } = this.props;
                            await MessageHandler.initPushNotification();
                            this.notificationForegroundListener = MessageHandler.foregroundNotificationListener(navigation);
                            this.notificationBackgroundListener = MessageHandler.backgroundNotificationListener(navigation);

                            this.setState({ loading: false, data });
                            }

                            componentWillUnmount() {
                            this.notificationForegroundListener();
                            this.notificationBackgroundListener();
                            }
                            async componentDidMount() {
                            MessageHandler.requestPermission();
                            AppState.addEventListener('change', this.handleAppStateChange);
                            MessageHandler.clearBadges();
                            }

                            componentWillUnmount() {
                            AppState.removeEventListener('change', this.handleAppStateChange);
                            }
                            handleAppStateChange = (nextAppState) => {
                            if (nextAppState.match(/inactive|background/)) {
                            MessageHandler.clearBadges();
                            }
                            ....


                            I hope this give you an Idea how to implement it for your needs.






                            share|improve this answer












                            I think you are fine with the "how firebase notification work"... cause of this, here is only an description of the Logic how you can Deeplinking into your App.



                            If you send a notification, add a data-field. Let's say your app has a Tab-Navigator and the sections "News","Service" and "Review".
                            In your Push-Notification - Datafield (let's name it "jumpToScreen" you define your value:




                            jumpToScreen = Service




                            I assume you still have the Handling to recieve Notifications from Firebase implemented.
                            So create an /lib/MessageHandler.js Class and put your business-logic inside.



                            import firebase from 'react-native-firebase';

                            /*
                            * Get a string from Firebase-Messages and return the Screen to jump to
                            */
                            const getJumpPoint = (pointer) => {
                            switch (pointer) {
                            case 'News':
                            return 'NAV_NewsList'; // <= this are the names of your Screens
                            case 'Service':
                            return 'NAV_ServiceList';
                            case 'Review':
                            return 'NAV_ReviewDetail';
                            default: return false;
                            }
                            };

                            const MessageHandler = {
                            /**
                            * initPushNotification initialize Firebase Messaging
                            * @return fcmToken String
                            */
                            initPushNotification: async () => {
                            try {
                            const notificationPermission = await firebase.messaging().hasPermission();
                            MessageHandler.setNotificationChannels();

                            if (notificationPermission) {
                            try {
                            return await MessageHandler.getNotificationToken();
                            } catch (error) {
                            console.log(`Error: failed to get Notification-Token n ${error}`);
                            }
                            }
                            } catch (error) {
                            console.log(`Error while checking Notification-Permissionn ${error}`);
                            }
                            return false;
                            },
                            clearBadges: () => {
                            firebase.notifications().setBadge(0);
                            },
                            getNotificationToken: () => firebase.messaging().getToken(),
                            setNotificationChannels() {
                            try {
                            /* Notification-Channels is a must-have for Android >= 8 */
                            const channel = new firebase.notifications.Android.Channel(
                            'app-infos',
                            'App Infos',
                            firebase.notifications.Android.Importance.Max,
                            ).setDescription('General Information');

                            firebase.notifications().android.createChannel(channel);
                            } catch (error) {
                            console.log('Error while creating Push_Notification-Channel');
                            }
                            },
                            requestPermission: () => {
                            try {
                            firebase.messaging().requestPermission();
                            firebase.analytics().logEvent('pushNotification_permission', { decision: 'denied' });
                            } catch (error) {
                            // User has rejected permissions
                            firebase.analytics().logEvent('pushNotification_permission', { decision: 'allowed' });
                            }
                            },
                            foregroundNotificationListener: (navigation) => {
                            // In-App Messages if App in Foreground
                            firebase.notifications().onNotification((notification) => {
                            MessageHandler.setNotificationChannels();
                            navigation.navigate(getJumpPoint(notification.data.screen));
                            });
                            },
                            backgroundNotificationListener: (navigation) => {
                            // In-App Messages if App in Background
                            firebase.notifications().onNotificationOpened((notificationOpen) => {
                            const { notification } = notificationOpen;
                            notification.android.setChannelId('app-infos');
                            if (notification.data.screen !== undefined) {
                            navigation.navigate(getJumpPoint(notification.data.screen));
                            }
                            });
                            },
                            appInitNotificationListener: () => {
                            // In-App Messages if App in Background
                            firebase.notifications().onNotificationOpend((notification) => {
                            notification.android.setChannelId('app-infos');
                            console.log('App-Init: Da kommt ne Message rein', notification);
                            firebase.notifications().displayNotification(notification);
                            });
                            },
                            };

                            export default MessageHandler;


                            In your index.js you can connect it like this:



                            import MessageHandler from './lib/MessageHandler';
                            export default class App extends Component {
                            state = {
                            loading: null,
                            connection: null,
                            settings: null,
                            };
                            async componentDidMount() {
                            const { navigation } = this.props;
                            await MessageHandler.initPushNotification();
                            this.notificationForegroundListener = MessageHandler.foregroundNotificationListener(navigation);
                            this.notificationBackgroundListener = MessageHandler.backgroundNotificationListener(navigation);

                            this.setState({ loading: false, data });
                            }

                            componentWillUnmount() {
                            this.notificationForegroundListener();
                            this.notificationBackgroundListener();
                            }
                            async componentDidMount() {
                            MessageHandler.requestPermission();
                            AppState.addEventListener('change', this.handleAppStateChange);
                            MessageHandler.clearBadges();
                            }

                            componentWillUnmount() {
                            AppState.removeEventListener('change', this.handleAppStateChange);
                            }
                            handleAppStateChange = (nextAppState) => {
                            if (nextAppState.match(/inactive|background/)) {
                            MessageHandler.clearBadges();
                            }
                            ....


                            I hope this give you an Idea how to implement it for your needs.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 days ago









                            suther

                            2,80912043




                            2,80912043






















                                up vote
                                0
                                down vote



                                accepted










                                My solution was to use add what information I need in the data object of the notification message object:



                                in functions/index.js:



                                  let message = {
                                notification: {
                                body: `new notification `
                                },
                                token: pushToken,
                                data: {
                                type: 'NEW_TRAINING',
                                title: locationTitle
                                }
                                };


                                and process as follows in the app for navigation:



                                this.notificationOpenedListener =
                                firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
                                if (notification.data.type === 'NEW_TRAINING') {
                                this.props.navigator.push({
                                screen: 'newtrainingscreen',
                                title: notification.data.title,
                                animated: true
                                });
                                }





                                share|improve this answer

























                                  up vote
                                  0
                                  down vote



                                  accepted










                                  My solution was to use add what information I need in the data object of the notification message object:



                                  in functions/index.js:



                                    let message = {
                                  notification: {
                                  body: `new notification `
                                  },
                                  token: pushToken,
                                  data: {
                                  type: 'NEW_TRAINING',
                                  title: locationTitle
                                  }
                                  };


                                  and process as follows in the app for navigation:



                                  this.notificationOpenedListener =
                                  firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
                                  if (notification.data.type === 'NEW_TRAINING') {
                                  this.props.navigator.push({
                                  screen: 'newtrainingscreen',
                                  title: notification.data.title,
                                  animated: true
                                  });
                                  }





                                  share|improve this answer























                                    up vote
                                    0
                                    down vote



                                    accepted







                                    up vote
                                    0
                                    down vote



                                    accepted






                                    My solution was to use add what information I need in the data object of the notification message object:



                                    in functions/index.js:



                                      let message = {
                                    notification: {
                                    body: `new notification `
                                    },
                                    token: pushToken,
                                    data: {
                                    type: 'NEW_TRAINING',
                                    title: locationTitle
                                    }
                                    };


                                    and process as follows in the app for navigation:



                                    this.notificationOpenedListener =
                                    firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
                                    if (notification.data.type === 'NEW_TRAINING') {
                                    this.props.navigator.push({
                                    screen: 'newtrainingscreen',
                                    title: notification.data.title,
                                    animated: true
                                    });
                                    }





                                    share|improve this answer












                                    My solution was to use add what information I need in the data object of the notification message object:



                                    in functions/index.js:



                                      let message = {
                                    notification: {
                                    body: `new notification `
                                    },
                                    token: pushToken,
                                    data: {
                                    type: 'NEW_TRAINING',
                                    title: locationTitle
                                    }
                                    };


                                    and process as follows in the app for navigation:



                                    this.notificationOpenedListener =
                                    firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
                                    if (notification.data.type === 'NEW_TRAINING') {
                                    this.props.navigator.push({
                                    screen: 'newtrainingscreen',
                                    title: notification.data.title,
                                    animated: true
                                    });
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered yesterday









                                    Nicoara Talpes

                                    189116




                                    189116






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52150199%2fdeep-links-in-react-native-firebase-notifications%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest




















































































                                        Popular posts from this blog

                                        Full-time equivalent

                                        さくらももこ

                                        13 indicted, 8 arrested in Calif. drug cartel investigation