如何在点击通知时跳转到通知屏幕

How to jump to notification screen when click on notification

我用 firebase 设置了通知系统并响应本机。它工作正常,但是当我在应用程序的任何状态(已终止、前或后台)收到通知并单击通知时,都会将我推送到主屏幕。当有人点击通知时,我想进入通知屏幕。 怎么可能?

首先,您必须在 top-level 组件中添加一个 Firebase 通知侦听器。我建议将它放在 App.js.

  1. Foreground Listener
  2. Background Listener

之后,您必须借助导航道具导航到特定屏幕。如果您将侦听器放置在 App.js 处,那么您将无法访问导航道具,因为 App.js 将 return 导航容器。 为此,您必须创建一个这样的辅助函数 navigation without prop

例如

export const App = () => {
  messaging().setBackgroundMessageHandler(async remoteMessage => {
      navigate('Notification'); //navigate to notification screen
  });
  const unsubscribe = messaging().onMessage(async remoteMessage => {
    //generate local notification using your preferred library
    //handle navigation

  })
   
  return <RootNavigator />;
};