即使在 iOS 中发送了通知,setBackgroundMessageHandler 也不会返回任何内容

问题描述 投票:0回答:2

我正在使用 @react-native-firebase/messaging 进行推送通知,但是, 由于某种原因,当我使用模拟器和真实的 iPhone 进行调试时,即使已在 iOS 中设置了通知,我也无法让 setBackgroundMessageHandler 打印 console.log。我尝试按照 Firebase 云消息传递教程进行操作。

import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import App from './src/App';
import {name as appName} from './app.json';

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

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

我把所有后台获取、远程通知和后台处理都安排好了。任何帮助将不胜感激

react-native firebase-cloud-messaging
2个回答
0
投票

首先,您需要配置

headless

无头文档

  • 示例
// index.js
import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';

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

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

  return <App />;
}

function App() {
  // Your application
}

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

其次,对于 iOS 特定的“仅数据”消息,该消息必须包含适当的 APN 标头以及

content-available
标志才能触发后台处理程序。例如,如果使用 Node.js firebase-admin 包向 iOS 设备发送“仅数据”消息:

  • 示例
admin.messaging().send({
  data: {
    //some data
  },
  apns: {
    payload: {
      aps: {
        contentAvailable: true, // add this !!
      },
    },
    headers: {
      'apns-push-type': 'background',
      'apns-priority': '5',
      'apns-topic': '', // your app bundle identifier
    },
  },
  //must include token, topic, or condition
  //token: //device token
  //topic: //notification topic
  //condition: //notification condition
});

0
投票

我现在明白这个问题了。您似乎面临着在 iOS 上处理处于终止状态的通知的挑战,特别是与使用适当的 APN 标头和内容可用标志发送“仅数据”消息以触发后台处理程序有关。

如果您希望在终止或终止状态下收到通知时执行任务或进行 API 调用,则应考虑发送两个单独的通知。第一个通知应包含带有通知部分的有效负载,确保即使应用程序处于后台或终止状态,它也会触发 iOS 的通知弹出窗口。第二个通知将是“仅数据”消息,它必须包含必要的 APN 标头以及内容可用标志。此配置对于激活后台处理程序至关重要。

例如,当使用 Node.js firebase-admin 包向 iOS 设备发送“仅数据”消息时,请确保相应地构建有效负载。这种方法允许您实现在应用程序处于后台或终止状态时显示通知并触发后台处理程序进行进一步处理的所需行为。

注意有效负载结构的细节至关重要,包括可用内容和必要的 APN 标头的使用,以确保功能正常。建议在不同场景(包括前台、后台和终止状态)中进行测试,以验证您的实现是否按预期运行。

import messaging from '@react-native-firebase/messaging';
import React, { useEffect } from 'react';
import { AppRegistry, AppState } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
  console.log('====================================');
  console.log({ 'in kill state': remoteMessage });
  // api call for status update
// you can perform you desired task
});
function HeadlessCheck({ isHeadless }) {
  console.log('background is called', isHeadless);
  if (isHeadless) {
    // App has been launched in the background by iOS, ignore
    return <FakeView />;
  }

  return <App />;
}

const FakeView = () => {
  return null;
};

AppRegistry.registerComponent(appName, () => App);
// Register the Headless task
AppRegistry.registerHeadlessTask(
  'RNFirebaseBackgroundMessage',
  () => HeadlessCheck,
);

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

    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',
    },
  },
};
© www.soinside.com 2019 - 2024. All rights reserved.