React navigation deep linking if the app is closed doesn't work

Viewed 389

When the application is open, deep linking works very well, but when closed it only opens my application.

When I check with alert, the data I want comes to the getInitialUrl part, but no data comes to the subscription. The last resort was to post here, I really don't understand. Your help is very important to me!

/**
Main.Activity.java
public class MainActivity extends ReactActivity {

  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(null);
   }
   @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
      return new ReactActivityDelegate(this, getMainComponentName()) {
        @Override
        protected ReactRootView createRootView() {
        return new RNGestureHandlerEnabledRootView(MainActivity.this);
        }
      };
  }
}
**/

Navigation

<NavigationContainer
                    theme={theme === 'dark' ? DarkTheme : DefaultTheme}
                    ref={navigationRef}
                    initialRouteName="App"
                    linking={{
                        prefixes: ["https://*****", "*****://"],
                        config,
                        async getInitialURL() {
                            // Check if app was opened from a deep link
                            const url = await Linking.getInitialURL();

                            if (url != null) {
                                return url;
                            }

                            // Check if there is an initial firebase notification
                            const message = await messaging().getInitialNotification();

                            // Get the `url` property from the notification which corresponds to a screen
                            // This property needs to be set on the notification payload when sending it
                            return message?.notification.url;
                        },
                        subscribe(listener) {
                            const onReceiveURL = ({ url }: { url: string }) => listener(url);

                            // Listen to incoming links from deep linking
                            Linking.addEventListener('url', onReceiveURL);

                            // Listen to firebase push notifications
                            const unsubscribeNotification = messaging().onNotificationOpenedApp(
                                (message) => {
                                    const url = message.notification.url;

                                    if (url) {
                                        // Any custom logic to check whether the URL needs to be handled
                                        //...

                                        // Call the listener to let React Navigation handle the URL
                                        listener(url);
                                    }
                                }
                            );

                            return () => {
                                // Clean up the event listeners
                                Linking.removeEventListener('url', onReceiveURL);
                                unsubscribeNotification();
                            };
                        },
                    }}
                >

AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.vis.smartiq">
        <queries>
          <package android:name="com.google.android.googlequicksearchbox"/>
        </queries>
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <application
          android:name=".MainApplication"
          android:label="@string/app_name"
          android:icon="@mipmap/ic_launcher"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:allowBackup="false"
          android:theme="@style/AppTheme">
          <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
            android:launchMode="singleTask"
            android:windowSoftInputMode="adjustResize"
            android:exported="true">
            <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="smartiq" />
                <data android:scheme="https" android:host="login.smartiq.io" />
            </intent-filter>
          </activity>
        </application>
    </manifest>

build.gradle

  // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        ext {
            buildToolsVersion = "30.0.2"
            minSdkVersion = 23
            compileSdkVersion = 31
            targetSdkVersion = 31
            ndkVersion = "21.4.7075529"
        }
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath('com.android.tools.build:gradle:7.0.0')
            classpath 'com.google.gms:google-services:4.3.10'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            mavenCentral()
            mavenLocal()
            maven {
                // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
                url("$rootDir/../node_modules/react-native/android")
            }
            maven {
                // Android JSC is installed from npm
                url("$rootDir/../node_modules/jsc-android/dist")
            }
    
            google()
            maven { url 'https://www.jitpack.io' }
        }
    }
0 Answers
Related