gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:29:55 26 4
gpt4 key购买 nike

  • 我不知道如何通过 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com