简体   繁体   中英

How to refetch data on click push notification using React-Native?

In my React-Native project I have implemented expo-push-notification module, I can say everything works as expected, but I have problem which I do not know how to solve, whenuser receives a message and clicks on it

1.I want to redirect to a specific screen 2. I want to refetch data and call specific API endpoint 3.I also want this feature to work when App is Foreground or Background

My project is on the Bare workflow.

After many research I could not find hint that would give me the right direction.

How realistic it is to achieve this goal with React-Native?

In your scenario, you'll need to use addNotificationReceivedListener, this listener will be triggered after the user clicks on the notification, inside that listener you can access the notification response data and navigate to another screen.

You can read more about it here https://docs.expo.dev/push-notifications/receiving-notifications/

Example of the notification listener function

const handleNotificationResponse = (response: NotificationResponse) => {
    const { data } = response.notification.request.content
    if (data) {
        navigationRef?.current?.push('your_screen_name', {
            id: data?.id,
            otherImportantData: data.others,
          })
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM