应用程序在后台运行时,react-native深层链接

时间:2018-01-11 09:49:13

标签: react-native react-native-android deep-linking

我有我的Android应用程序(react-native)和深度链接第一次当我运行它而应用程序没有在后台运行时效果很好,但当应用程序在后台运行时我点击深 - 链接它甚至没有打开应用程序...我甚至不确定从哪里开始这个错误我在生命周期事件中尝试了一些console.logs但他们甚至不运行

请指导我在哪里查找问题以及如何解决问题,谢谢!

2 个答案:

答案 0 :(得分:0)

componentDidMount() {
    Linking.addEventListener('url', this._handleDeepLinkingURL);
    this._handleDeepLinkingURL(); /* Invoking method manually on launching app very first time */
},
componentWillUnmount() {
    Linking.removeEventListener('url', this._handleDeepLinkingURL);
},
_handleDeepLinkingURL(event) {
    /*
    console.log(event.url);
    event.url will hold the url of the app if the app launched when its running in background

    event param will be undefined when the app launched from kill state or first time
    */
    Linking.getInitialURL()
    .then(url => {
        if (url && url.indexOf("?params=") !== -1) {
            const paramsString = url.substr(
                url.indexOf("?params=") + 8
            );
            alert('paramsString is : ' + paramsString);
        }
    })
}

答案 1 :(得分:0)

即使应用程序在 background 中,深层链接也按预期工作。请检查以下规格。

<块引用>

节点版本:v12.18.x 或更大 NPM 版本:v6.14.x 或更大 反应原生cli:2.0.1 react-native : 0.63.x OR 更大

请检查您是否在 AppDelegate.m 中添加了以下行。

<块引用>

#import

它必须添加到 #ifdef FB_SONARKIT_ENABLED 行上方。在此行下方添加它会导致在存档以供发布时构建失败。

请检查您是否在负责深层链接的 AppDelegate.m 中添加了以下代码。

<块引用>

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 选项:(NSDictionary *)options { 返回 [RCTLinkingManager application:application openURL:url options:options]; }

它适用于应用冷启动,但如果您的应用在 background 中,它将不起作用。为此,您需要在 AppDelegate.m

中添加以下代码 <块引用>

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restoreHandler:(nonnull void (^)(NSArray *_Nullable))restorationHandler { 返回 [RCTLinkingManager 应用程序:应用程序 继续用户活动:用户活动 恢复处理程序:恢复处理程序]; }

无论您的 AppState 如何,这都应该有效:active **OR** background

这符合我的预期!试试看。这绝对是可行的。

提前致谢!