Plugins
Dynamic Links
@nativescript/firebase-dynamic-links
- Intro
- Set up Firebase and the Dynamic Links SDK
- Use @nativescript/firebase-dynamic-links to create and receive dynamic links
- Receive Dynamic Links
- API
- License
Intro
This plugin allows you to add the Firebase Dynamic Links SDK to your app, create dynamic links and receive links that launch a screen on your app.
Set up Firebase and the Dynamic Links SDK
To set up and initialize Firebase for your NativeScript app, follow the instructions on the documentation of the @nativescript/firebase-core plugin.
Install and import the
@nativescript/firebase-dynamic-links
to add the Dynamic Links SDK.Install the plugin by running the following command in the root directory of your project.
clinpm install @nativescript/firebase-dynamic-links
To add the Dynamic Links SDK, import the
@nativescript/firebase-dynamic-links
plugin. You should import the plugin once in your app project and the ideal place to do that is the app bootstrapping file(app.ts
,main.ts
, etc).
Tip For an optimal experience with Dynamic Links, the Firebase Dynamic Links documentation recommends you to enable Google Analytics by adding the Firebase SDK for Google Analytics to your app. To add and use Firebase SDK for Google Analytics in your NativeScript app, see the @nativescript/firebase-analytics plugin documentation.
Add a URL prefix for your Dynamic Links with the following steps:
- Go to the Firebase Console
- Click on
Dynamic Link
- Click on
Get started
and enter your URL prefix in the form that follows.
Recommended: Specify the URL patterns allowed in your deep links and fallback links. For more information, refer to Allow specific URL patterns.
iOS: Confirm that your Firebase project is properly configured
For instructions to confirm that your Firebase project is properly configured to use Dynamic Links for iOS, see configuration step 4.
Use @nativescript/firebase-dynamic-links to create and receive dynamic links
Create a dynamic link from parameters
You can create a dynamic link via the Firebase console, your app or even your custom API. To create a dynamic link from parameters with the plugin, call the buildLink method on the DynamicLinks class instance passing it a literal object of parameters or an instance of DynamiclinkParameters returned by the createLink or createShortLink method.
ShortLinkType
enum ShortLinkType {
DEFAULT = 'default',
SHORT = 'short',
UNGUESSABLE = 'unguessable',
}
buildLink()
dynamicLinks = firebase().dynamicLinks()
link: string = dynamicLinks.buildLink(linkParameters)
Builds a dynamic link from parameters and returns the link as a string
. Use the returned link to direct the user to your desired content.
Parameter | Type | Description |
---|---|---|
linkParameters | DynamicLinkParameters | The dynamic link parameters used to create a dynamic link. |
onLink()
dynamicLinks: DynamicLinks = firebase().dynamicLinks()
listener = (link: DynamicLink | null, error: FirebaseError | null) => {
// handle the link event
}
dynamicLinks.onLink(listener)
Allows you to add a callback function that gets called when your app's screen is launched by a dynamic link.
Parameter | Type | Description |
---|---|---|
listener | OnLinkListener | The function to be called when the app's screen is launched by a dynamic link. |
OnLinkListener type
type OnLinkListener = (
link: DynamicLink | null,
error: FirebaseError | null
) => void
resolveLink()
dynamicLinks: DynamicLinks = firebase().dynamicLinks()
dynamicLinks.resolveLink(link).then((dynamicLink: DynamicLink) => {})
Create the parameters of a dynamic link
As mentioned in the Create a Dynamic Link from parameters section above, you can create a dynamic link's parameters with the createLink
or createShortLink
method. To create a dynamic link's parameters, call either of the methods with the deep link(the link to open when the dynamic link is clicked) and your URL prefix as the first and second arguments, respectively.
const dynamicLinks = firebase().dynamicLinks()
const link = dynamicLinks.createShortLink(
'https://docs.nativescript.org',
'https://triniwiz.page.link'
)
link.social = new DynamicLinkSocialParameters()
link.social.imageUrl =
'https://art.nativescript.org/logo/export/NativeScript_Logo_White_Blue_Rounded.png'
let dynamiLink = await dynamicLinks.buildLink(link)
Receive Dynamic Links
Android: Add intent filters for deep links
To receive a dynamic link with a deep link to your scheme, add an intent-filter
to your app's AndroidManifest.xml
file located at app/App_Resources/src/main
as follows.
<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="YOUR_SCHEME" />
</intent-filter>
</activity>
Add a callback function to receive the links
To receive the deep link that launches a screen of your app, call the onLink method on an instance of the DynamicLinks class with the function to be notified with the link.
const dynamicLinks = firebase().dynamicLinks()
dynamicLinks.onLink((link: DynamicLink) => {
console.log('onLink', link)
})
API
DynamicLinks class
ios
dynamicLinks = firebase().dynamicLinks()
dynamicLinksIOS: FIRDynamicLinks = firebase().dynamicLinks().ios
A readonly
property that returns the iOS instance of FIRDynamicLinks wrapped by the DynamicLinks
class.
android
dynamicLinks = firebase().dynamicLinks()
dynamicLinksAndroid = firebase().dynamicLinks().android
A readonly
property that returns the Android instance of FirebaseDynamicLinks wrapped by the DynamicLinks
class.
app
dynamicLinks = firebase().dynamicLinks()
dynamicLinksApp: FirebaseApp = firebase().dynamicLinks().app
A readonly
property that returns the FirebaseApp instance.
createLink()
dynamicLinks = firebase().dynamicLinks()
dynamicLinkParameters: DynamicLinkParameters = dynamicLinks.createLink(
link,
domainUri
)
Creates parameters for a dynamic link and returns a DynamicLinkParameters object to be passed to the method to create a dynamic link.
Parameter | Type | Description |
---|---|---|
link | string | The link the app will open when the dynamic link is tapped. For more information, see the description at Dynamic Link parameters. |
domainUriPrefix | string | Your domain prefix registered under Dynamic Links on Firebase Console. |
createShortLink()
dynamicLinks = firebase().dynamicLinks()
dynamicLinkParameters: DynamicLinkParameters = dynamicLinks.createShortLink(
link,
domainUri,
shortLinkType
)
Creates parameters for a dynamic link and returns a DynamicLinkParameters object to be passed to the method to create a dynamic link.
Parameter | Type | Description |
---|---|---|
link | string | The link the app will open when the dynamic link is tapped. For more information, see the description at Dynamic Link parameters. |
domainUriPrefix | string | Your domain prefix registered under Dynamic Links on Firebase Console. |
shortLinkType | ShortLinkType | Optional |
ShortLinkType
enum ShortLinkType {
DEFAULT = 'default',
SHORT = 'short',
UNGUESSABLE = 'unguessable',
}
buildLink()
dynamicLinks = firebase().dynamicLinks()
link: string = dynamicLinks.buildLink(linkParameters)
Builds a dynamic link from parameters and returns the link as a string
. Use the returned link to direct the user to your desired content.
Parameter | Type | Description |
---|---|---|
linkParameters | DynamicLinkParameters | The dynamic link parameters used to create a dynamic link. |
onLink()
dynamicLinks: DynamicLinks = firebase().dynamicLinks()
listener = (link: DynamicLink | null, error: FirebaseError | null) => {
// handle the link event
}
dynamicLinks.onLink(listener)
Allows you to add a callback function that gets called when your app's screen is launched by a dynamic link.
Parameter | Type | Description |
---|---|---|
listener | OnLinkListener | The function to be called when the app's screen is launched by a dynamic link. |
OnLinkListener type
type OnLinkListener = (
link: DynamicLink | null,
error: FirebaseError | null
) => void
resolveLink()
dynamicLinks: DynamicLinks = firebase().dynamicLinks()
dynamicLinks.resolveLink(link).then((dynamicLink: DynamicLink) => {})
Resolves the passed string and returns it as a DynamicLink if it's valid. Otherwise, it returns an error.
Parameter | Type | Description |
---|---|---|
link | string | The string to be resolved. |
DynamicLinkAnalyticsParameters class
Used to create Analytics parameters for a dynamic link.
ios
ios = dynamicLinkAnalyticsParameters.ios
--
android
android = dynamicLinkAnalyticsParameters.android
campaign
campaign: undefined | string = dynamicLinkAnalyticsParameters.campign
content
content: undefined | string = dynamicLinkAnalyticsParameters.content
// or
source
source: undefined | string = dynamicLinkAnalyticsParameters.source
term
term: undefined | string = dynamicLinkAnalyticsParameters.term
DynamicLinkParameters class
analytics
dynamicAnalytics: DynamicLinkAnalyticsParameters =
dynamicLinkParameters.analytics
android
dynamicLinkParametersAndroid: DynamicLinkAnalyticsParameters =
dynamicLinkParameters.android
ios
dynamicLinkParametersIOS: DynamicLinkAnalyticsParameters =
dynamicLinkParameters.ios
domainUriPrefix
dynamicDomainUriPrefix: string = dynamicLinkParameters.domainUriPrefix
The URL prefix of the dynamic link.
itunes
dynamicLinkITunesParameters: DynamicLinkITunesParameters =
dynamicLinkParameters.itunes
navigation
dynamicLinkNavigationParameters: DynamicLinkNavigationParameters =
dynamicLinkParameters.navigation
Gets or sets navigation info parameters.
social
dynamicLinkSocialParameters: DynamicLinkSocialParameters =
dynamicLinkParameters.social
DynamicLink object
This object represents data of the link received by your app.
ios
linkIOS: FIRDynamicLink = link.ios
android
linkAndroid: com.google.firebase.dynamiclinks.PendingDynamicLinkData =
link.android
minimumAppVersion
minimumAppVersion: string = link.minimumAppVersion
For the description of this property, see the description of getMinimumAppVersion() on the PendingDynamicLinkData class documentation.
url
url: string = link.url
For the description of this property, see the description of getUrl() on the PendingDynamicLinkData class documentation.
utmParameters
utmParameters: Record<string, string> = link.utmParameters
For the description of this property, see the description of getUtmParameters() on the PendingDynamicLinkData class documentation.
Resolves the passed string and returns it as a DynamicLink if it's valid. Otherwise, it returns an error.
Parameter | Type | Description |
---|---|---|
link | string | The string to be resolved. |
DynamicLinkAnalyticsParameters class
Used to create Analytics parameters for a dynamic link.
ios
ios = dynamicLinkAnalyticsParameters.ios
--
android
android = dynamicLinkAnalyticsParameters.android
campaign
campaign: undefined | string = dynamicLinkAnalyticsParameters.campign
content
content: undefined | string = dynamicLinkAnalyticsParameters.content
// or
source
source: undefined | string = dynamicLinkAnalyticsParameters.source
term
term: undefined | string = dynamicLinkAnalyticsParameters.term
DynamicLinkParameters class
analytics
dynamicAnalytics: DynamicLinkAnalyticsParameters =
dynamicLinkParameters.analytics
android
dynamicLinkParametersAndroid: DynamicLinkAnalyticsParameters =
dynamicLinkParameters.android
ios
dynamicLinkParametersIOS: DynamicLinkAnalyticsParameters =
dynamicLinkParameters.ios
domainUriPrefix
dynamicDomainUriPrefix: string = dynamicLinkParameters.domainUriPrefix
The URL prefix of the dynamic link.
itunes
dynamicLinkITunesParameters: DynamicLinkITunesParameters =
dynamicLinkParameters.itunes
navigation
dynamicLinkNavigationParameters: DynamicLinkNavigationParameters =
dynamicLinkParameters.navigation
Gets or sets navigation info parameters.
social
dynamicLinkSocialParameters: DynamicLinkSocialParameters =
dynamicLinkParameters.social
DynamicLink object
This object represents data of the link received by your app.
ios
linkIOS: FIRDynamicLink = link.ios
android
linkAndroid: com.google.firebase.dynamiclinks.PendingDynamicLinkData =
link.android
minimumAppVersion
minimumAppVersion: string = link.minimumAppVersion
For the description of this property, see the description of getMinimumAppVersion() on the PendingDynamicLinkData class documentation.
url
url: string = link.url
For the description of this property, see the description of getUrl() on the PendingDynamicLinkData class documentation.
utmParameters
utmParameters: Record<string, string> = link.utmParameters
For the description of this property, see the description of getUtmParameters() on the PendingDynamicLinkData class documentation.
License
Apache License Version 2.0