Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring a React Native Android app to the foreground when it is in the background or has been killed?

Is there any way to bring a React Native app to the foreground when it is in the background or has been killed?

I have tried using react-native-invoke-app but it is no longer being maintained and only supports Android versions < 9.

like image 340
Mr. Robot Avatar asked Nov 07 '22 12:11

Mr. Robot


1 Answers

I think the best way to do this is to set a URL scheme and opening the URL connected to the app.

1 - Add this line of code

<data android:scheme="my-awesome-app" /> in ./android/app/src/main/AndroidManifest.xml under the <intent-filter> under <activity> tag.

This will give a scheme to your app which is useful in deepLinking and opening your app with a browser or while you're in other apps. Actually if you open the my-awesome-app:// url, you're app will be opened.

2 - Write a headless task or an event listener to run Linking.openURL(my-awesome-app://);

This is an example from react-native-firebase message listener.

import firebase from 'react-native-firebase';
import {Linking} from 'react-native';

firebase.messaging().onMessage((message) => {
    // Process your message as required
    Linking.openURL('my-awesome-app://')
  });
}

To set the URL scheme setting on iOS, you must add it in URL types in info.plist file like the picture below.

iOS setting up URL scheme

like image 100
Amir Gorji Avatar answered Nov 14 '22 23:11

Amir Gorji