Share Content Between Users

One of the most effective ways to get new users to install your app is by enabling your users to share content from your app with their friends. With Dynamic Links, you can create a great user-to-user sharing experience: users who receive content recommendations from their friends can click a link and be taken directly to the shared content in your app, even if they have to go to the App Store or Google Play Store to install your app first.

Key benefits

  • New users opening your app for the first time get a first-run experience that you customize for them. For example, you can display the content that was shared with them, or automatically connect them with the friend that invited them.
  • Makes it easy for users to share content with their friends across platforms and whether or not their friends have your app installed.

Here's how to get started!

Set up Firebase and the Dynamic Links SDK

Set up a new Firebase project and install the Dynamic Links SDK into your app. (iOS, Android, C++, Unity). Installing the Dynamic Links SDK allows Firebase to pass along data about the Dynamic Link to the app, including after the user installs the app. Without the SDK, there's no way to connect a post-install user with a pre-install click.

Create Dynamic Links

Now it's time to set up the links that users can send to their friends. Don't worry if your users' friends don't have the app installed yet; Dynamic Links can take care of that for you.

For each element of content you want to be shareable, generate a Dynamic Link.

When you create the Dynamic Link, you'll need to provide an HTTP or HTTPS URL as the link parameter that will be used to identify the content you're sharing. If you have a website with equivalent content, you should use your website's URLs. This will ensure that these links render correctly on a platform that doesn't support Dynamic Links, such as a desktop browser. For example:

https://example.page.link/?link=https://www.example.com/content?item%3D1234&apn=com.example.android&ibi=com.example.ios&isi=12345

You can also send a data payload—for example, to indicate that the link is intended for a particular person, such as in a game invitation—by adding URL-encoded parameters to the URL.

https://example.page.link/?link=https://www.example.com/invitation?gameid%3D1234%26referrer%3D555&apn=com.example.android&ibi=com.example.ios&isi=12345

Before you share these links, you might want to use the Firebase Dynamic Links URL shortener API to generate friendlier-looking URLs. A short Dynamic Link looks like the following example:

https://example.page.link/WXYZ

Whatever you send the full link or a shortened link, when users open the Dynamic Link on their device, if the app specified by the apn parameter (on Android) or the ibi and isi parameters (on iOS) isn't installed, users are taken to the Play Store or App Store to install the app. Then, the app opens, and the URL specified in the link parameter is passed to it.

Add "Share" buttons that send Dynamic Links

Next, add "Share" buttons to your app's content that send Dynamic Links to your user's friends using the app of your user's choice. The recipient of the user's suggestion can open the Dynamic Link and see the shared content, possibly after installing the app first.

iOS

On iOS, your "Share" button might present a UIActivityViewController:

Objective-C

NSString *myDynamicLink = LINK_TO_SHARE;
NSString *msg = [NSString stringWithFormat:@"Hey, check this out: %@", myDynamicLink];
UIActivityViewController *shareSheet =
    [[UIActivityViewController alloc] initWithActivityItems:@[ msg ]
                                      applicationActivities:nil];
[shareSheet popoverPresentationController].sourceView = self.view
[self presentViewController:shareSheet animated:YES completion:nil];

Swift

let myDynamicLink = LINK_TO_SHARE;
let msg = "Hey, check this out: " + myDynamicLink
let shareSheet = UIActivityViewController(activityItems: [ msg ], applicationActivities: nil)
shareSheet.popoverPresentationController?.sourceView = self.view
self.presentViewController(shareSheet, animated: true, completion: nil)

Android

On Android, your "Share" button starts an activity with the ACTION_SEND intent:

Intent sendIntent = new Intent();
String msg = "Hey, check this out: " + myDynamicLink;
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
sendIntent.setType("text/plain");
startActivity(sendIntent);

Open the linked content in your app

Finally, you need to receive the link that's passed to your app so you can display the linked content to the recipient. This is easy using the Dynamic Links SDK:

iOS

On iOS, you receive the Dynamic Link by implementing the application:continueUserActivity:restorationHandler: method. In the restoration handler, you can get the Dynamic Link by calling handleUniversalLink:completion:. If a Dynamic Link was passed to your app, you can get it from the url property of the FIRDynamicLink. For example:

Objective-C

[[FIRDynamicLinks dynamicLinks]
    handleUniversalLink:userActivity.webpageURL
             completion:^(FIRDynamicLink * _Nullable dynamicLink,
                          NSError * _Nullable error) {
      NSString *link = dynamicLink.url;
      BOOL strongMatch = dynamicLink.matchConfidence == FIRDynamicLinkMatchConfidenceStrong;
      // ...
    }];

Swift

FIRDynamicLinks.dynamicLinks()?.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
    let link = dynamicLink.url
    let strongMatch = dynamicLink.matchConfidence == FIRDynamicLinkMatchConfidenceStrong
    // ...
}

Additionally, you must call dynamicLinkFromCustomSchemeURL: in the application:openURL:options: method to receive Dynamic Links passed to your app as custom scheme URLs. For example:

Objective-C

FIRDynamicLink *dynamicLink = [[FIRDynamicLinks dynamicLinks] dynamicLinkFromCustomSchemeURL:url];
if (dynamicLink) {
  NSString *link = dynamicLink.url;
  BOOL strongMatch = dynamicLink.matchConfidence == FIRDynamicLinkMatchConfidenceStrong;
  // ...
  return YES;
}

Swift

let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLinkFromCustomSchemeURL(url)
if let dynamicLink = dynamicLink {
  let link = dynamicLink.url
  let strongMatch = dynamicLink.matchConfidence == FIRDynamicLinkMatchConfidenceStrong
  // ...
  return true
}

Now that you have the value of the link parameter, you can display the linked content to the recipient, or process the data specified by the parameter in some other way. A URL-routing library such as JLRoutes can help with this task.

If you're receiving a link intended for a specific recipient, ensure that the Dynamic Link's match confidence is strong before running any user-specific logic.

Android

On Android, you use the getInvitation() method to get data from the Dynamic Link:

AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, false).setResultCallback(/* ... */);

Then, in the callback, you can get the data passed in the Dynamic Links link parameter by calling the getDeepLink() method:

String link = AppInviteReferral.getDeepLink(intent);

Now that you have the value of the link parameter, you can display the linked content to the recipient, or process the data specified by the parameter in some other way. A URL-routing library can help with this task.

Send feedback about...

Need help? Visit our support page.