[英]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?当 React Native 应用程序在后台或被杀死时,有什么方法可以将它带到前台?
I have tried using react-native-invoke-app but it is no longer being maintained and only supports Android versions < 9.我曾尝试使用react-native-invoke-app但它不再被维护并且仅支持 Android 版本 < 9。
I think the best way to do this can be setting a URL scheme and opening the url connected to the app.我认为最好的方法是设置 URL 方案并打开连接到应用程序的 url。
1- add this line of code 1-添加这行代码
<data android:scheme=“my-awesome-app” />
in ./android/app/src/main/AndroidManifest.xml
under the <intent-filter>
under <activity>
tag. <data android:scheme=“my-awesome-app” />
在<activity>
标签下的<intent-filter>
下的./android/app/src/main/AndroidManifest.xml
中。
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.实际上,如果您打开
my-awesome-app://
url,您的应用程序将被打开。
2- write a headless task or an event listener to run the Linking.openURL(my-awesome-app://);
2-编写无头任务或事件侦听器来运行
Linking.openURL(my-awesome-app://);
this is an example from react-native-firebase message listener.这是 react-native-firebase 消息监听器的一个例子。
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 方案设置,您必须将其添加到
info.plist
文件中的 URL 类型中,如下图所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.