当 React Native Android 应用程序在后台或已被杀死时,如何将其带到前台?

Mr.*_*bot 6 java android react-native

当 React Native 应用程序在后台或已被杀死时,有什么方法可以将其带到前台吗?

我尝试过使用react-native-invoke-app,但它不再维护并且仅支持Android版本<9。

Ami*_*rji 3

我认为最好的方法是设置 URL 方案并打开连接到应用程序的 URL。

1 - 添加这行代码

<data android:scheme="my-awesome-app" />在下标签./android/app/src/main/AndroidManifest.xml下。<intent-filter><activity>

这将为您的应用程序提供一个方案,该方案在深层链接和使用浏览器打开您的应用程序或在其他应用程序中时非常有用。实际上,如果您打开该my-awesome-app://网址,您的应用程序就会被打开。

2 - 编写一个无头任务或一个事件监听器来运行Linking.openURL(my-awesome-app://);

这是消息侦听器的示例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://')
  });
}
Run Code Online (Sandbox Code Playgroud)

要在 iOS 上设置 URL 方案设置,您必须将其添加到info.plist文件中的 URL 类型中,如下图所示。

iOS 设置 URL 方案