Reputation: 1824
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.
Upvotes: 13
Views: 3375
Reputation: 1824
The reason this doesn't work is that react-native-invoke-app
doesn't support Android versions > 8.
Upvotes: 4
Reputation: 3335
instead of invokeapp();
you can describe a url scheme and to open the url related to that scheme, to make the app get opened and come in foreground.
ON Android:
add <data android:scheme=“my-awesome-app” />
intent in ./android/app/src/main/AndroidManifest.xml
under the ` tag.
ON iOS
open your xcode, bring the iOS project up and open Info.plist
file in xcode.
add URL types
row under the root key, then add an item e.g: Item 0
, then URL Schemes
then an item with a string value. (in our example it's my-awesome-app).
Just like the picture below
Now after setting things the url scheme up, now you can call it like this.
import {Linking} from 'react-native';
Linking.openURL('my-awesome-app://');
you can use it everywhere you'd like.
Upvotes: 3
Reputation: 932
Using react-native-invoke-app
should get the job done.
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessaging);
on your root application component.backgroundMessaging
from whichever child component you have it in and import it to your app root component.I have not tested this but i dont see a reason why it should not work.
Hope this Helps!
Upvotes: 1