[React Native][IOS] 如何通过通知打开应用程序时捕获事件

[React Native][IOS] How to catch the event when the app is opened by a notification

React Native 有一个 PushNotificationIOS 模块来获取通知数据并监听 notificaitonregister 等事件。我想在通过以下通知打开应用程序时捕获一个事件:PushNotificationIOS.addEventListener('notification_open', myhandler);。有办法吗?

你有两个选择。

1。使用 PushNotificationIOS

您可以在重要的时候调用 PushNotificationIOS.getInitialNotification();。例如,您可以在应用程序的 index.js 文件中调用此方法并决定要呈现的组件。来自 the docs:

This method returns a promise. If the app was launched by a push notification, this promise resolves to an object of type PushNotificationIOS. Otherwise, it resolves to null.

2。使用本机 iOS App Delegate

来自 the UIApplicationDelegate documentation:

For example, if your app was launched because of an incoming remote notification, you might want to reconfigure your user interface to display data related to that notification. For a list of possible reasons why your app might be launched, see Launch Options Keys.

如果您出于某种原因需要更早地了解,您可以查看本机 application(_:willFinishLaunchingWithOptions:)application(_:didFinishLaunchingWithOptions:) 方法。请注意,这些方法是在 React Native 初始化之前调用的。这是大多数 React Native 应用不需要的更高级的用例。