Mr. Robot
Mr. Robot

Reputation: 1824

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.

Upvotes: 6

Views: 4077

Answers (1)

Amir Gorji
Amir Gorji

Reputation: 3335

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

Upvotes: 3

Related Questions