I am successfully using Firebase Cloud Messaging with react-native-firebase to send and receive data only messages between Android devices.
When a device receives a message, if the app is in the background or has been killed I would like to bring the app to the foreground. I am getting notifications that will display a console.log()
under both circumstances, but I'm having trouble figuring out how to best approach 'waking up' the app / bring it to the foreground:
firebase.messaging().onMessage((message) => {
// Process your message as required
console.log('You got a message!', message);
});
}
The above code is executed as expected under both circumstances.
Are there React Native packages I can look at to help achieve what I'm trying to do or should I be considering having to write some Java?
I am trying to achieve something like this but not sure if it's possible to add code like this straight in to my React Native application?
I have discovered this package react-native-invoke-app that uses HeadlessJS.
HeadlessJS requires you to use this code in your index.js
:
AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
However Firebase Cloud Messaging
already needs you to provide this line of code in the same place, which I'm guessing does the same thing as above:
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessaging);
Following the react-native-invoke-app
docs I am then placing the call to invoke my app inside the firebase.messaging().onMessage((message))
callback like so:
firebase.messaging().onMessage((message) => {
// Process your message as required
invokeApp();
console.log('You got message!', message);
});
}
However this is not bringing my app to the foreground.
I've now moved the invokeApp()
call inside the Firebase backgroundNotificationListener
function which is a headless task which seems like the correct approach:
import invokeApp from 'react-native-invoke-app';
const incomingMessage = async (message) => {
// handle your message
invokeApp();
return Promise.resolve();
}
export default incomingMessage;
But the app will still not come to the foreground when the incomingMessage
function is called.
I have raised this issue on the react-native-invoke-app
github page which has more details on the issues I'm facing.
So as a solution you can use react-native-push-notification to fire push notification when app in foreground. For android you don't need to follow any native installation steps just install library by this command and then you can fire local push notification as below : Create a file called NotificationController.
So the basic idea is just add RemotePushController component above the ending root tag. That's it!!!! Now you are good to go. Now just run your react-native app again and try sending push notifications while your app is running in foreground.
The reason this doesn't work is that react-native-invoke-app
doesn't support Android versions > 8. If anyone knows an alternative to this package please comment below, thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With