5

I am creating a function to store data through async-storage at FCM's setBackgroundMessageHandler event.

I am using @react-native-firebase/messaging. SetBackgroundMessageHandler is not working.

I followed the tutorial at https://rnfirebase.io/messaging/usage/ios-setup and sent it from the firebase website.

Everything works fine except for the current setBackgroundMessageHandler function.

I've been looking for it for a long time, but I can't find the answer.

I tested it with a real device. The push message is displayed normally, but the setBackgroundMessageHandler event does not occur.

Xcode BackgroundMode ScreenShot

package.json

"@react-native-firebase/app": "^11.2.0",
"@react-native-firebase/messaging": "^11.2.0",
"@react-native-firebase/ml": "^11.2.0",

index.js

import React from 'reactn';
import messaging from '@react-native-firebase/messaging';
import {AppRegistry} from 'react-native';
import App from './App';

messaging().setBackgroundMessageHandler(async remoteMessage => {
  // ...is not working
  console.log('Message handled in the background!', remoteMessage);
});

function HeadlessCheck({isHeadless}) {
  if (isHeadless) {
    return null;
  }

  return <App />;
}

AppRegistry.registerComponent('AppName', () => HeadlessCheck);

App.tsx

  React.useEffect(() => {
    var _resultList = resultList;

    const unsubscribe = messaging().onMessage(async remoteMessage => {
      //...is working
    }

  messaging().onNotificationOpenedApp(remoteMessage => {
    //...is working
  });

  // Check whether an initial notification is available
  messaging()
    .getInitialNotification()
    .then(remoteMessage => {
      if (remoteMessage) {
        //...is working
      }
    });

  }, []);
k22pr
  • 189
  • 1
  • 9

1 Answers1

2

There was no problem with my code, and the cloud messaging testing tool provided by Firebase did not arrive properly.

Successfully generated token via OAuth 2.0 Playground and sent message to postman. https://developers.google.com/oauthplayground

The code below is the structure that successfully transferred to postman. https://fcm.googleapis.com/v1/projects/[firebase-project-id]/messages:send

{
    "message": {
        "token": "device token",
        "notification": {
            "title": "message title",
            "body": "hello background worker?"
        },
        "apns": {
            "payload": {
                "aps": {
                    "content-available": 1
                }
            }
        }
    }
}
k22pr
  • 189
  • 1
  • 9