【问题标题】:[React Native][IOS] How to catch the event when the app is opened by a notification[React Native][IOS] 应用被通知打开时如何捕捉事件
【发布时间】:2018-08-12 21:45:05
【问题描述】:

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

【问题讨论】:

    标签: ios react-native push-notification react-native-ios


    【解决方案1】:

    你有两个选择。

    1。使用PushNotificationIOS

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

    此方法返回一个承诺。如果应用程序是通过推送通知启动的,则此承诺将解析为 PushNotificationIOS 类型的对象。否则,它会解析为null

    2。使用原生 iOS App Delegate

    来自the UIApplicationDelegate documentation

    例如,如果您的应用因收到远程通知而启动,您可能需要重新配置用户界面以显示与该通知相关的数据。有关启动您的应用的可能原因列表,请参阅启动选项键。

    如果出于某种原因需要提前知道,可以签入原生的application(_:willFinishLaunchingWithOptions:)application(_:didFinishLaunchingWithOptions:) 方法。请注意,这些方法在 React Native 初始化之前被调用。这是大多数 React Native 应用程序不需要的更高级的用例。

    【讨论】:

    • PushNotificationIOS.getInitialNotification() 在用户点击通知或在后台收到通知后直接打开应用程序时调用。您如何区分这两种情况?
    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多