android - 当应用程序处于后台状态时,深度链接不起作用 React native
问题描述
我正在创建基于反应原生的电子商务应用程序。这里我需要从 url shared 打开单个产品页面。实际上,当应用程序处于终止状态时它会工作,但如果应用程序处于后台/非活动状态,它将无法工作。在后台/非活动状态下打开时,共享 url 为空。我已附加我的代码。
// following code working for app killing state
componentWillMount() {
if (Platform.OS === 'android') {
console.log("Testing");debugger
//Constants.OneTimeFlag == false;
Linking.getInitialURL().then(url => {
console.log(url);
var str = url
var name = str.split('/')[4]
Constants.isLinking = true;
this.setState({ shop_Id: name})
if (str)
{
this.setState({ isFromHomeLinking:'FROM_LINK' })
this.props.navigation.navigate('SingleProductScreen', { ListViewClickItemHolder: [this.state.shop_Id,1,this.state.isFromHomeLinking] });
}
});
}
else {
Linking.addEventListener('url', this.handleNavigation);
}
}
Not working code following..
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
this.state.appState declared in constructor(props)
_handleAppStateChange = (nextAppState) => {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
console.log('App has come to the foreground!');debugger
if (Platform.OS === 'android') {
console.log("Testing");debugger
//Constants.OneTimeFlag == false;
Linking.getInitialURL().then(url => {
console.log(url);
var str = url
var name = str.split('/')[4]
Constants.isLinking = true;
this.setState({ shop_Id: name})
if (str)
{
this.setState({ isFromHomeLinking:'FROM_LINK' })
this.props.navigation.navigate('SingleProductScreen', { ListViewClickItemHolder: [this.state.shop_Id,1,this.state.isFromHomeLinking] });
}
});
}
else {
Linking.addEventListener('url', this.handleNavigation);
}
}
}
当我在后台状态 Linking.getInitialURL() 中打开来自 whatsapp 和应用程序的外部链接时,收到为 null ..
以下我在清单文件中
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
android:host="demo1.zgroo.com" />
</intent-filter>
</activity>
以下是我的示例网址..
请让我知道任何解决方案..
提前致谢..
解决方案
您需要为这种情况注册链接侦听器。
componentDidMount() {
Linking.addEventListener('url', this._handleOpenURL);
},
componentWillUnmount() {
Linking.removeEventListener('url', this._handleOpenURL);
},
_handleOpenURL(event) {
console.log(event.url);
}
推荐阅读
- javascript - 在 Cloud Function 事务中检索并分配 gameId 给多个玩家
- wagtail - 使用 id 作为 slug 创建页面后,Wagtail 管理消息中的“查看实时”URL 错误
- swift - 导航栏中带有按钮的 SwiftUI 选择器
- sql - SQL:连续行之间的差异
- javascript - React 组件未呈现为 HTML
- encoding - 在 RPI 问题上使用 OMX RTMP/FLV 编码 H264
- python - 如何更新 tensorflow 2.0 中的镜像变量?
- c# - 如何分离 IObservable 和 IObserver
- c++ - 在 C++ 中实现 unix 实用程序“head”
- r - 如何检查字符串在R中是否为日期时间格式?