legato_centralize_deep_link 0.0.14
legato_centralize_deep_link: ^0.0.14 copied to clipboard
Centralize deep link
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.
Handling Multiple Deep Link Formats #
This plugin supports two formats for deep links:
- Format 1:
https://{deeplinkdev-dynamic-csw.legato.co}/{token}using for most of the case - 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.
Step 1: Add Deep Link Configuration #
<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>
iOS Configuration #
To enable deep linking on iOS, follow these steps:
1. Adjust Info.plist #
- Open
ios/Runner/Info.plistin Xcode or a text editor. - Add the following entry to enable Flutter deep linking:
<key>FlutterDeepLinkingEnabled</key>
<true/>
2. Add “Associated Domains” Capability #
- Open your project in Xcode (
ios/Runner.xcworkspace). - Select your app target, go to the Signing & Capabilities tab.
- Click the
+ Capabilitybutton and add Associated Domains. - Add your domain(s) in the format:
applinks:deeplinkdev-dynamic-{name}.legato.co
3. Host the apple-app-site-association File #
- Place a valid
apple-app-site-associationfile athttps://deeplinkdev-dynamic-{name}.legato.co/apple-app-site-association. - The file must be served with
Content-Type: application/jsonand no file extension. - Example content:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "<TeamID>.<BundleIdentifier>",
"paths": ["*"]
}
]
}
}
4. Test Deep Links #
- Run your app on a simulator or real device.
- You can also test using the Xcode CLI:
xcrun simctl openurl booted "https://deeplinkdev-dynamic-{name}.legato.co/{token}"
Refer to Apple documentation for more details.