I don't know how to get receive message in background by React Native (only for Android)
I simply want to receive the latest message in Android then show up on screen
Now it only can receive in the foreground.
I followed 2 links but still can't overcome this problem
https://www.npmjs.com/package/react-native-android-sms-listener
https://www.npmjs.com/package/react-native-background-job
This is my code
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);
Hope anyone giving a solution or a different sample, tutorial,... to solve this! Thank you all in advance!