I'm working on Chat app using React native and WebSocket
everything works fine in active mode but when you push the home button to make the app in background mode, the WebSocket
onMessage events function is not triggered
The good thing is that WebSocket
connection is still connected but the events function not triggered.
I just want to push the notification when receiving a message in the background mode.
I did a research and I found that I need to run a silent background audio track at all times(some said this illegal way).
Is there a legal API to keep a connection alive in the background?
Do I need to re-connect the socket connection in the background mode
My code
events = (data) =>{
if(data[0].message){
if(this.state.appState !== 'active'){
console.log('check here') // not working when the app in background mode
PushNotification.localNotification({// not working when the app in background mode
message: data[0].message,
number: 1,
title: 'a new message from: '+data[0].username,
});
}else{
this.setState({messages: data[0]})
}
}
}
socketConnect = () =>{
AsyncStorage.getItem('token').then((token) => {
let connection = new wamp.Connection({ url: 'wss://*******/',
realm: 'realm',
authmethods: ['jwt'],
});
connection.onopen = (session, detalis) => {
session.subscribe('messages', this.events);
};
connection.open();
})
};