You can create short or long Dynamic Links with the Firebase Dynamic Links API. The API takes several optional parameter structures to build links. Short links can also be created from a previously generated long link. Firebase Dynamic Links generates a URL like the following:
https://example.page.link/WXYZ
The C++ SDK works for both Android and iOS, with some additional setup required for each platform.
Before you begin
Android
- If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
- In the Firebase console, open the Dynamic Links section.
If you have not already accepted the terms of service and set a domain for your Dynamic Links, do so when prompted.
If you already have a Dynamic Links domain, take note of it. You need to provide a Dynamic Links domain when you programmatically create Dynamic Links.
- Add Firebase to your Android project.
- Add the dependency for Firebase Dynamic Links to your app-level
build.gradle
file:dependencies { implementation 'com.google.firebase:firebase-dynamic-links:16.1.5' }
- Link the
libapp.a
andlibdynamic_links.a
static library, from the C++ SDK.
iOS
- If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
- In the Firebase console, open the Dynamic Links section.
If you have not already accepted the terms of service and set a domain for your Dynamic Links, do so when prompted.
If you already have a Dynamic Links domain, take note of it. You need to provide a Dynamic Links domain when you programmatically create Dynamic Links.
- Add Firebase to your iOS project.
- The Firebase Dynamic Links C++ client library uses custom URL schemes on iOS
to process links. You must add custom URL schemes to your app to support
receiving Dynamic Links:
- To open your project configuration, double-click the project name in the left tree view. Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section.
- Click the + button, and add a URL scheme for your reversed client
ID. To find this value, open the
GoogleService-Info.plist
configuration file, and look for theREVERSED_CLIENT_ID
key. Copy the value of that key, and paste it into the URL Schemes box on the configuration page. Leave the other fields blank. - Click the + button, and add a second URL scheme. This one is the
same as your app's bundle ID. For example, if your bundle ID is
com.example.ios
, type that value into the URL Schemes box. You can find your app's bundle ID in the General tab of the project configuration (Identity > Bundle Identifier).
- Include the following Pod in your
Podfile
:pod 'Firebase/DynamicLinks'
- Run
pod install
- Add
firebase.framework
andfirebase_dynamic_links.framework
, from the C++ SDK, to your Xcode project.
Use the Firebase console
If you want to generate a single Dynamic Link, either for testing purposes, or for your marketing team to easily create a link that can be used in something like a social media post, the simplest way would be to visit the Firebase console and create one manually following the step-by-step form.
Using the Firebase Dynamic Links API
Create and initialize App
Before you can create Dynamic Links, you'll need to create and initialize
a firebase::App
object.
Include the header file for firebase::App
:
#include "firebase/app.h"
The next part varies depending on your platform:
Android
Create the firebase::App
, passing the JNI environment and a jobject
reference to the Java Activity as arguments:
app = ::firebase::App::Create(::firebase::AppOptions("APPLICATION NAME"), jni_env, activity);
iOS
Create the firebase::App
:
app = ::firebase::App::Create(::firebase::AppOptions("APPLICATION NAME"));
Initialize Dynamic Links library
Before creating a Dynamic Link, you must first initialize the Dynamic Links library:
::firebase::dynamic_links::Initialize(app, null);
Creating a long Dynamic Link from parameters
To create a Dynamic Link, create a DynamicLinkComponents object, setting any of
the optional members for additional configuration, and passing it to
dynamic_links::GetShortLink
or dynamic_links::GetLongLink
.
The following minimal example creates a long Dynamic Link to https://www.example.com/ that opens with your Android app com.example.android.package_name and iOS app com.example.ios:
firebase::dynamic_links::IOSParameters ios_parameters("com.example.ios"); firebase::dynamic_links::AndroidParameters android_parameters( "com.example.android.package_name"); firebase::dynamic_links::DynamicLinkComponents components( "https://www.example.com/", "example.page.link"); components.android_parameters = &android_parameters; components.ios_parameters = &ios_parameters; firebase::dynamic_links::GeneratedDynamicLink long_link = firebase::dynamic_links::GetLongLink(components);
Creating a short Dynamic Link
To create a short Dynamic Link, pass a previously generated long link to
GetShortLink
or build DynamicLinkComponents
the same way as above.
GetShortLink
optionally takes an extra DynamicLinkOptions
config
parameter with PathLength
; this allows you to control how the link should be
generated. Generating a short link requires a network request to the Firebase
backend, so GetShortLink
is asynchronous, returning a Future<GeneratedLink>
.
For example:
firebase::dynamic_links::DynamicLinkOptions short_link_options; short_link_options.path_length = firebase::dynamic_links::kPathLengthShort; firebase::Future<firebase::dynamic_links::GeneratedDynamicLink> result = firebase::dynamic_links::GetShortLink(components, short_link_options);
If your program has an update loop that runs regularly (say at 30 or 60 times per second), you can check the results once per update:
if (result.status() == firebase::kFutureStatusComplete) { if (result.error() == firebase::dynamic_links::kErrorCodeSuccess) { firebase::dynamic_links::GeneratedDynamicLink link = *result.result(); printf("Create short link succeeded: %s\n", link.url.c_str()); } else { printf("Created short link failed with error '%s'\n", result.error_message()); } }