Jakub G.
Jakub G.

Reputation: 23

react-native-firebase: onNotificationOpenedApp and getInitialNotification not working on iOS

I'm using Cloud Messaging from react-native-firebase 7.0.1.

I do not want to store iOS device tokens in firebase, so I'm using getAPNSToken() method and then storing it on my backend.

I'm able to send notification on a device and after pressing it, the application opens.

But when I'm trying to get message from getInitialNotification() or onNotificationOpenedApp() it always returns null/undefined.

It works every time on 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;
  }
}

Upvotes: 2

Views: 7160

Answers (2)

febaisi
febaisi

Reputation: 684

It used to work before. I mean, for the RNFirebase 5.x.x versions. For some reason, the maintainers of the library decided to only send those events up to the JS layer when it's a Firebase Cloud Message coming through.

Here is the IF statement:
https://github.com/invertase/react-native-firebase/blob/master/packages/messaging/ios/RNFBMessaging/RNFBMessaging%2BUNUserNotificationCenter.m#L89

enter image description here

I removed the IF statement it seems to be working as before. But yeah, that's the new behavior of the RN-Firebase library.

Upvotes: 3

Louie Anderson
Louie Anderson

Reputation: 181

Just realized that according to the docs, these functions only work when sending and receiving notifications via FCM, which is why you aren't seeing these work when sending them manually through APNS.

This package will allow for leverage getInitialNotification and the other function: https://github.com/react-native-community/push-notification-ios

Upvotes: 5

Related Questions