react-native deep linking while app is running in the background
Asked Answered
O

2

10

I have my android app (react-native based) and the deep link first time when I run it while the app is not running in the background works great, but when the app runs in the background and i click on the deep-link it doesn't even open the app ... I'm not sure even where to start with this bug I tried a few console.logs in lifecycle events but they don't even run.

please guide me where should I look for the issue and how to fix it, thanks!

Octuple answered 11/1, 2018 at 9:49 Comment(4)
I face same issue. Did you get workaround yet?Spinneret
Did you add Linking.removeEventListener("url", this.handleOpenURL), function at componentWillUnmount()? Can you remove ti and try again?Benumb
I face same issue to, have you find the solution for this?Deserved
Please check the below link for a simple solution; #62694260Armington
I
2

The deep linking is working as expected for me even the app is in background. Please check the below specifications.

Node Version : v12.18.x OR Greater NPM Version : v6.14.x OR Greater react-native-cli : 2.0.1 react-native : 0.63.x OR Greater

Please check if you have added below line in your AppDelegate.m.

#import <React/RCTLinkingManager.h>

It must be added above #ifdef FB_SONARKIT_ENABLED line. Adding it below this line will cause failing of build while Archiving it for release.

Please check if you have added below code in your AppDelegate.m which is responsible for deep linking.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { return [RCTLinkingManager application:application openURL:url options:options]; }

It will work for app cold boot, but it will not work if your app is in background. For this, you need to add below code in your AppDelegate.m

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray<id> *_Nullable))restorationHandler { return [RCTLinkingManager application:application continueUserActivity:userActivity restorationHandler:restorationHandler]; }

This should work irrespective of your AppState: active **OR** background.

This worked for me as expected! Give it a try. This is should definitely work.

Thanks in advance!

Iatry answered 3/5, 2021 at 8:4 Comment(1)
Thanks. application continueUserActivity did the trick for iOSSuffering
D
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);
        }
    })
}
Downstroke answered 7/11, 2019 at 6:40 Comment(2)
Please try and add a little detail about what your code does and how it solves the problem. It will help everyone else in future.Carma
Please check the below link for a simple solution; #62694260Armington

© 2022 - 2024 — McMap. All rights reserved.