omar shady
omar shady

Reputation: 171

open flutter app using specific URL(Deep Linking)

I have just developed an eCommerce app using flutter framework, I am trying to share specific URL for any product so the customer can open this product details screen in the app using the shared URL. I have used uni_links package to handle with this link, but I don't know what is the problem because the app doesn't open when I open the URL in the browser, here are the codes that I used.

1- AndroidManifest.xml

<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="ecards"
    android:icon="@mipmap/ic_launcher">
   
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
        <meta-data
          android:name="io.flutter.embedding.android.SplashScreenDrawable"
          android:resource="@drawable/launch_background"
          />
        <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="unilinks"
      android:host="example.com" />
  </intent-filter>
    </activity>
    <!-- Don't delete the meta-data below.
         This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />
</application>

2- uni_links code that is used to handle URL

import 'dart:async';
import 'package:uni_links/uni_links.dart';

StreamSubscription _sub;

Future<Null> initUniLinks() async {
  _sub = getLinksStream().listen((String link) {
    print(link);
  }, onError: (err) {
    print(err);
  });
}

3- Main file in Flutter project

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initUniLinks();
  await translator.init(
    localeDefault: LocalizationDefaultType.device,
    languagesList: <String>['ar', 'en'],
    assetsDirectory: 'assets/langs/',
  );

note: I am using Xiaomi mi 9t to test the app

Upvotes: 2

Views: 3547

Answers (1)

dokkaebi
dokkaebi

Reputation: 91

It seems like there are a few things that may be going wrong.

First, for testing the links in Android, you will want to use adb from the command-line rather than typing in the link in a browser, e.g.:

adb shell am start -a android.intent.action.VIEW \
    -c android.intent.category.BROWSABLE \
    -d "http://flutterbooksample.com/book/1"

Second, there does not appear to be any code handling the link once it is received from the stream. You will need such code to open the appropriate route in your app.

If you are only looking to support Android, I would recommend simply following the guide here: https://flutter.dev/docs/development/ui/navigation/deep-linking and make sure you have the appropriate routes set up for the links that you want to handle.

If you want to support iOS as well, I was able to handle incoming links with uni_links by using a combination of code from https://morioh.com/p/89f560dcaabd for when the app is launched from a link and https://loic-ngou.medium.com/deep-linking-in-flutter-app-using-uni-links-4efc14412187 for when the app is already launched.

For the 1st, I used a function like the following to pass in the uri to my app and called it from main:

Future checkForLaunchFromLink() async {
  try {
    Uri? initialUri = await UniLink.getInitialUri();
    runApp(new MyApp(uri: initialUri));
  } on PlatformException {
    print("PlatformException");
  } on Exception {
    print('Exception thrown');
  }
}

For the 2nd, I used a StreamBuilder for home: in my MaterialApp to route appropriately when the app was already open.

Upvotes: 1

Related Questions