首页 > 解决方案 > react-native-firebase:onNotificationOpenedApp 和 getInitialNotification 在 iOS 上不起作用

问题描述

我正在使用 react-native-firebase 7.0.1 中的云消息传递。

我不想将 iOS 设备令牌存储在 Firebase 中,所以我使用getAPNSToken()方法,然后将其存储在我的后端。

我可以在设备上发送通知,按下它后,应用程序就会打开。

但是当我试图从中获取消息时,getInitialNotification()onNotificationOpenedApp()总是返回null/ undefined

它每次都可以在 Android 上运行。

PushHandler.js

import React from 'react';
import messaging from '@react-native-firebase/messaging';

export default class PushHandler extends React.Component {
  constructor(props) {
    super(props);
    this.messageApp = messaging();
  }

  componentDidMount() {
    this.messageApp.onNotificationOpenedApp(remoteMessage => {
      if (remoteMessage) { // <- always null on iOS
        // handle notification
      }
    });

    this.messageApp.getInitialNotification().then(initialMessage => {
      if (initialMessage) { // <- always undefined on iOS
        // handle notification
      }
    });
  }
  render() {
    return null;
  }
}

标签: iosfirebasereact-nativepush-notificationreact-native-firebase

解决方案


刚刚意识到,根据文档,这些功能仅在通过 FCM 发送和接收通知时才有效,这就是为什么您在通过 APNS 手动发送通知时看不到这些功能的原因。

这个包将允许利用 getInitialNotification 和其他功能:https ://github.com/react-native-community/push-notification-ios


推荐阅读