【问题标题】:Headless task in react native android appliation反应本机android应用程序中的无头任务
【发布时间】:2019-06-22 12:18:34
【问题描述】:

我目前正在使用无头任务来接收推送通知并显示本地通知。我目前正在使用 react-native-firebase 来集成 firebase 云消息传递。


export default bgMessaging = (message) => {
    console.log('hello', message)
    return Promise.resolve();
}

这是我用来创建任务的一段代码。

在 index.js 中

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); 

我正在使用它来注册HeadlessTask。

但是当应用程序处于终止状态时,我没有收到任何控制台消息,如果我的任务正在运行,也不会收到。

需要帮助以了解如何调试后台任务并显示已终止状态的自定义通知。

【问题讨论】:

  • 我想知道你不需要使用 HeadlessTask。 React Native Firebase 有另一个 API(通知),当应用程序在后台运行时总是会收到消息。 rnfirebase.io/docs/v5.x.x/notifications/introduction
  • 不太确定,我想自定义远程通知,只能这样。但是因为我在这里没有收到任何处理程序。我想详细调试一下。
  • @cauchy 你有没有找到调试无头 js 任务的解决方案,或者你有没有处理后台任务
  • 我正在尝试通过它播放声音,但似乎不起作用。有没有找到解决办法。

标签: android react-native react-native-firebase


【解决方案1】:

我是这样解决的:

// index.js

import { AppRegistry, Alert } from 'react-native';
import messaging from '@react-native-firebase/messaging';

import App from './App';
import { name as appName } from './app.json';

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

//receive the message in the background
messaging().onMessage(async teste => {
  console.log('teste:', teste);

  const novo = (title, message) => {
    return Alert.alert(title, message);
  };
  novo(teste.notification.title, teste.notification.body);
});


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

这是因为我们必须在应用后台验证推送消息何时到达

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-27
    • 2021-06-16
    • 2017-10-30
    • 2021-04-20
    • 2023-03-29
    • 1970-01-01
    • 2021-01-20
    • 2022-01-22
    相关资源
    最近更新 更多