Expo notifiaction, where do we add the push notification listeners?

Please provide the following:

  1. SDK Version: 40.0.1
  2. Platforms(Android/iOS/web/all): Android
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

Hello, I have read all the documentations on how to set up expo push notification from setup, to sending, to receiving. However I am confused on the receiving documentation. I am currently tryig to use expo notifications and am developing in the bare workflow environment.

Currently I intend on making two types of push notification. One from my backend for updates of the application, and another one as a scheduled push notification where the application gives the user a notification at a designated time (ex. 2021-06-30 14:15 pm).

I can more or less understand sending the notifications but I am confused on how I add the listeners. Unlike other listeners that may be unique to individual screens, to my understanding the notification listener should be global since it should be ready for it at any point. In other words the listener should be applied throughout the entire time the app is open or closed, regardless of which screen you are in.

Where should we add this notification listener to achieve this effect? It does not mention in the documentation where we are to add the listener. My initial thought was that it should be added at the top-level of the application, which is the “app.js” file. But here I define the react native navigation containers, stacks, and tab navigators and cannot think of where the listener would fit in such a context.

So what is the proper place to add the notification listener? And how would we do that using useEffect in a react function component instead of what is documented (using componentDidMount() in the react class component)?

Thank you

The example in the docs uses a functional component, so that should answer your useEffect question.

As for where to add it- yeah, I would suggest adding it in your root component (so App.js/ts). What do you mean you can’t think of where the listener would fit in that context?

Thank you for your response! I will try what you have told me and will let you know :slight_smile:

1 Like

Hello,

I have successfully implemented the push notification but am having a little trouble handling the notification responses.

As advised, I have put the listenres in the app.js root component with useEffect. However I am confused about navigating to screens at this level. In my app.js, I initialize and define the stack and tab navigators, along with the navigation container. There is no inherited navigation prop at this root level so how would I handle the notification to go to a specific screen?

Thank you :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.