简体   繁体   English

如何使用 React-Native 重新获取点击推送通知的数据?

[英]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在我的React-Native项目中,我实现了expo-push-notification模块,我可以说一切都按预期工作,但是当用户收到一条消息并点击它时,我遇到了我不知道如何解决的问题

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 1.我想重定向到一个特定的屏幕 2.我想重新获取数据并调用特定的 API 端点 3.我也希望这个功能在 App 是前台或后台时工作

My project is on the Bare workflow.我的项目在 Bare 工作流程上。

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?用 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.在您的场景中,您需要使用 addNotificationReceivedListener,此侦听器将在用户单击通知后触发,在该侦听器内您可以访问通知响应数据并导航到另一个屏幕。

You can read more about it here https://docs.expo.dev/push-notifications/receiving-notifications/你可以在这里阅读更多关于它的信息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,
          })
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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