简体   繁体   中英

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.

I think the best way to do this can be setting 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 the 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 设置 url 方案

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM