Centralize Deep Link Plugin for Flutter

A Flutter plugin for generating and managing centralized deep link URLs. This library provides methods to create a centralized deep link URL and to extract tokens from it, enabling smooth and secure navigation within your Flutter app.

Features

  • Generate Centralized Deep Link URL: Create a URL that links to specific content or features in your app.
  • Extract Token from Deep Link URL: Retrieve a unique token from the deep link URL, which can then be used to query metadata and perform additional actions.

Installation

To install this plugin, add it as a dependency in your pubspec.yaml file:

dependencies:
  centralize_deep_link_plugin: ^1.0.0

Here's the raw code for the README in Markdown format:

markdown Copy code

Centralize Deep Link Plugin for Flutter

A Flutter plugin for generating and managing centralized deep link URLs. This library provides methods to create a centralized deep link URL and to extract tokens from it, enabling smooth and secure navigation within your Flutter app.

Features

  • Generate Centralized Deep Link URL: Create a URL that links to specific content or features in your app.
  • Extract Token from Deep Link URL: Retrieve a unique token from the deep link URL, which can then be used to query metadata and perform additional actions.

This plugin supports two formats for deep links:

  1. Format 1: https://{deeplinkdev-dynamic-csw.legato.co}/{token} using for most of the case
  2. Format 2: https://{deeplinkdev-dynamic-keeper.legato.co}/token/{token} using when user install new app from store, and open the application via store.

Installation

To install this plugin, add it as a dependency in your pubspec.yaml file:

dependencies:
  centralize_deep_link_plugin: ^1.0.0

Then, run:

flutter pub get

Usage Import the library:

import 'package:centralize_deep_link_plugin/centralize_deep_link_plugin.dart';

Generate Centralized Deep Link URL To create a centralized deep link URL, use the generateDeepLink method. This method takes a token and returns a URL that can be shared or used to navigate to specific content within the app.

final _config = const DeepLinkConfiguration(
    projectId: '{PROJECT_ID}',
    apiKey: '{API_KEY}',
    secret: '{SECRET}');

final _env = DeepLinkEnvironment.dev;
 Future<void> _generateDeepLink() async {
    String generateLink;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      generateLink = await DeepLink().generateDeepLink(
          _config,
          _env,
          const GenerateLinkRequest(metadata: {
            "page_type": "product",
            "page_id": 1,
            "page_title": "Aliquam erat volutpat."
          }, pageUrl: 'https://placeholder.com/'));
    } on PlatformException {
      generateLink = 'Failed to generate deep link';
    } on Exception {
      generateLink = 'On Exception';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
    setState(() {
      _deepLink = generateLink;
    });
  }

Extract Token from Deep Link URL To extract the token from a deep link URL, use the extractTokenFromUrl method. This will return the meta data, which can be used to query or perform other actions.

import 'package:deep_link/deep_link.dart';
final _config = const DeepLinkConfiguration(
    projectId: '{PROJECT_ID}',
    apiKey: '{API_KEY}',
    secret: '{SECRET}');

final _env = DeepLinkEnvironment.dev;
Future<void> _extraMetadata(String token) async {
  Map<String, dynamic> metaData = {};
  // Platform messages may fail, so we use a try/catch PlatformException.
  // We also handle the message potentially returning null.
  try {
    final metaRequest = {
      'is_click': false,
      'is_redirect': true,
      'is_flutter': true
    };
    metaData = await DeepLink().extractLinkMetaData(_config, _env, token,
        metaData: metaRequest);
  } on PlatformException {
    _metaData = 'Failed to generate deep link';
  } on Exception {
    _metaData = 'On Exception';
  }

  // If the widget was removed from the tree while the asynchronous platform
  // message was in flight, we want to discard the reply rather than calling
  // setState to update our non-existent appearance.
  if (!mounted) return;
  setState(() {
    _metaData = jsonEncode(metaData);
  });
}

Android Configuration

To handle deep links on Android, add the following intent filter in your AndroidManifest.xml file under the <activity> tag for your main activity.

<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="https" />
    <data android:host="@string/centralize_host" />
</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="https" />
<data android:host="@string/centralize_host" />
<data android:pathPrefix="/token" />
</intent-filter>

Step 2: Define centralize_host in res/values/strings.xml

Open the res/values/strings.xml file and add the following line:

<string name="centralize_host">deeplinkdev-dynamic-{name}.legato.co</string>