setBackgroundMessageHandler:当应用程序在 iOS 上处于退出状态时如何调用

setBackgroundMessageHandler: How to invoke when app is in quit state on iOS

我正在使用 react-native-firebase 库。

在 iOS,我可以在前台、后台以及应用处于退出状态时接收通知。处理程序也适用于所有场景,除了一个场景。

我唯一的问题是:当应用程序处于退出状态时,setBackgroundMessageHandler 没有被调用,即使我收到了通知。我也尝试发送纯数据消息,但结果是一样的。

我什至可以在消息的 apns 部分使用 content-available: 1 在无头状态下调用应用程序。

但是 setBackgroundMessageHandler 仍然没有被调用。这是我的 index.js 页面:

messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('HEADLESS BACKGROUND: ' + ' | ' + JSON.stringify(remoteMessage));
});

function HeadlessCheck({ isHeadless }) {
  if (isHeadless) {
    console.log("Headless");
    // App has been launched in the background by iOS, ignore
    return null;
  }

  return <App />;
}

AppRegistry.registerComponent(appName, () => HeadlessCheck);

在无头调用的情况下,returning null 不是 return 一个不包含任何内容且不呈现任何内容的简单组件。

Index.js

messaging().setBackgroundMessageHandler(async (remoteMessage) => {
  console.log('HEADLESS BACKGROUND: ' + JSON.stringify(remoteMessage));
});

function HeadlessCheck({ isHeadless }) {
  if (isHeadless) {
    console.log('Headless');
    return <AppFake />;
    /* Notice this component, it is not the App Component but a different one*/
  }

  return <App />;
}

AppRegistry.registerComponent(appName, () => HeadlessCheck);

AppFake 组件仅包含以下内容:

import { useEffect } from 'react';
import SplashScreen from 'react-native-splash-screen';

const AppFake = () => {
  useEffect(() => {
    SplashScreen.hide();
  }, []);

  return null;
};

export default AppFake;

我确实不得不隐藏 SplashScreen。这对我来说很重要。

这是我从服务器发送的负载:

var admin = require('firebase-admin');

const message = {
  notification: {
    title: title,
    body: body,
  },
  data: { campaignId: campaignId, title: title, body: body },
  token: registrationId,
  android: {
    priority: 'high',
    notification: {
      title: title,
      body: body,
      sound: 'default',
    },
  },
  apns: {
    payload: {
      aps: {
        sound: 'default',
        'content-available': 1,
      },
    },
    headers: {
      'apns-priority': '5',
    },
  },
};

let response = admin
  .messaging()
  .send(message)
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });

现在,即使应用程序在 iOS 处于退出状态,也会对每条新消息调用 setBackgroundMessageHandler