2017-07-24 76 views
1
  • 我不知道如何在後臺接收郵件的陣營本地 (僅適用於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); 

希望任何人給一個解決方案或不同的樣本,導師我......解決這個問題! 謝謝大家!

回答

-1

你應該SmsListenerModule.java

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

}

看待這個問題從球員誰作出反應本地短信監聽https://github.com/CentaurWarchief/react-native-android-sms-listener/issues/6

希望它可以幫助你對此有何評論unregisterReceiver(mReceiver)

+0

雖然此鏈接可能回答問題,但最好在此處包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/16837214) – msanford

+0

對不起,我的第一個答覆在這裏。剛編輯它。 –

+0

你真的救了我的命! –