background - 如何在前台后台接收消息 - React Native?

标签 background message react-native-android

  • 我不知道如何通过 React Native 在后台获取接收消息
    (仅适用于安卓)
  • 我只想在 Android 中接收最新消息然后显示
    屏幕上
  • 现在它只能在前台接收。
  • 我关注了 2 个链接,但仍然无法解决这个问题

  • https://www.npmjs.com/package/react-native-android-sms-listener

    https://www.npmjs.com/package/react-native-background-job

    这是我的代码
    import React, { Component } from 'react';
    import {
      AppRegistry,
      Text,
      View
    } from 'react-native';
    
    import BackgroundJob from 'react-native-background-job';
    import SmsListener from 'react-native-android-sms-listener';
    
    /*
    Register background job with jobKey
    */
    const myJobKey = 'Hej';
    
    BackgroundJob.register({
      jobKey: myJobKey,
      job: () => console.log('Background Job fired!')
    });
    
    export default class ListenMessageApp extends Component {
    
      //constructor include last message
      constructor(props) {
        super(props);
        this.state = { lastMessage: 1 };
      }
    
      componentDidMount() {
        this.getAll();
        BackgroundJob.schedule({
          jobKey: myJobKey,
          period: 1000,
          timeout: 1000
        });
      }
    
      //Schedule function in background job
    
      getAll() {
        BackgroundJob.getAll({
          callback: () => {
           SmsListener.addListener(message => {
              this.setState({ lastMessage: message.body });
            });
          }
        });
      }
    
      render() {
        return (
          <View>
            <Text> Scheduled jobs: {this.state.lastMessage} </Text>
          </View>
        );
      }
    }
    
    AppRegistry.registerComponent('ListenMessageApp', () => ListenMessageApp);
    

    希望任何人提供解决方案或不同的示例、教程、...来解决这个问题!
    谢谢大家!

    最佳答案

    你应该评论 unregisterReceiver(mReceiver)在 SmsListenerModule.java 中

    public void onHostPause() {
     //unregisterReceiver(mReceiver);
    

    }

    看看制作 react-native-sms-listener 的人的这个问题 https://github.com/CentaurWarchief/react-native-android-sms-listener/issues/6

    希望对你有帮助。

    关于background - 如何在前台后台接收消息 - React Native?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273616/

    相关文章:

    Android 到 C++ 套接字连接在收到文件后关闭

    react-native - 使用react-native-paytm时无法发布版本

    css - 我们如何在 react-native 中设计 Arrow 卡

    background - Mathematica 网格中的多个背景子区域

    html - 背景图片在移动设备下方留下空白

    ios - 进入后台状态时关闭 UIAlertViews

    react-native - React-Native构建失败无法下载protobuf-java-3.4.0.jar(com.google.protobuf:protobuf-java:3.4.0)

    ios - 减少 iOS 后台位置电池使用

    android - 无法使用 smack 接收群组消息

    version-control - 在版本控制系统中编写更改消息的指南?