Does react native have a function lifecycle is running when app is not use or in background mode?
I need a function lifecycle in react-native is working when user not using the app or app in background mode. I want to check the database for getting the notification componentDidMount is work but the user must open the app and close it again
I need a function or lifecycle is always working
This is my notification code:
import React, { Component } from 'react';
import { View, NetInfo, Image, AppState, DeviceEventEmitter } from 'react-native';
import { Container, Text } from 'native-base';
import { RootNavigator } from './src/root';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import resducers from './src/reducers/index';
import PushController from'./PushController';
import PushNotification from 'react-native-push-notification';
import PushNotificationAndroid from 'react-native-push-notification';
const store = createStore(resducers);
export default class App extends Component<Props> {
constructor(Props){
super(Props);
this.state={
connection:null,
}
this.handleAppStateChange = this.handleAppStateChange.bind(this);
this.sendNotification = this.sendNotification.bind(this);
}
componentDidMount(){
AppState.addEventListener('change',this.handleAppStateChange);
}
componentWillMount(){
NetInfo.isConnected.addEventListener("connectionChange",this.handleConnectionChange);
NetInfo.isConnected.fetch().done((isConnected)=>this.setState({connection:isConnected}));
PushNotificationAndroid.registerNotificationActions(['Accept','Reject','Yes','No']);
DeviceEventEmitter.addListener('notificationActionReceived', function(e){
console.log ('notificationActionReceived event received: ' + e);
const info = JSON.parse(e.dataJSON);
if (info.action == 'Yes') {
alert('Accept');
} else if (info.action == 'No') {
alert('Reject')
}
// Add all the required actions handlers
});
}
componentWillUnMount(){
NetInfo.isConnected.removeEventListener("connectionChange",this.handleConnectionChange);
AppState.removeEventListener('change',this.handleAppStateChange);
}
handleConnectionChange=(isConnected)=>{
this.setState({connection:isConnected});
}
handleAppStateChange(appState){
if(appState==='background'){
PushNotification.localNotificationSchedule({
message:'Scheduled notification delay message',
date:new Date(Date.now()+(2000))
})
}
}
sendNotification(){
PushNotification.localNotification({
message:'You Pushed the notification button',
title:'My Notification Title',
ongoing:true,
vibrate:true,
playSound:true,
actions:'["Yes","No"]',
color:'red'
})
}
handeView(){
if(this.state.connection!==null && this.state.connection){
return <RootNavigator />
}else {
return <View style={{flex:1, alignItems:"center", justifyContent:"center"}}>
<Image source={require("./images/connection.gif")} style={{height: 150, width: 150, resizeMode : "stretch"}}/>
<Text style={{ fontFamily:"IRANSans", fontSize:18, textAlign:"center", color:"#b0b5bb" }}>لطفا اتصال اینترنت خود را بررسی کنید ...</Text>
</View>
}
}
render() {
return (
<Provider store={store}>
<Container>
{this.handeView()}
{this.sendNotification()}
</Container>
</Provider>
);
}
}
As @dentemm mentioned this need to run in the native code so you can try using this module https://github.com/jamesisaac/react-native-background-task
Note: For notifications usually we use an external service to push notifications to the user something like Firebase, Amazone SNS or Google Cloud Messages the notification will reach the user even if your app is completely closed because the OS will handle the notification and will open your app and run a function for you when the user clicks it, for more details you can check this tutorial https://medium.com/differential/how-to-setup-push-notifications-in-react-native-ios-android-30ea0131355e