Send and Receive Firebase Invites from Your Android App

Before you begin

  1. If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
  2. If you haven't yet enabled Firebase Dynamic Links, do so from the Firebase console by opening the Dynamic Links section and accepting the terms of service if prompted. Because Firebase Invites is built on Firebase Dynamic Links, you must enable Firebase Dynamic Links to use Firebase Invites.
  3. Add Firebase to your Android project.
  4. Add the dependency for Firebase Invites to your app-level build.gradle file:
    compile 'com.google.firebase:firebase-invites:11.6.2'
    

Send invitations

Start by building an Intent using the AppInviteInvitation.IntentBuilder class:

private void onInviteClicked() {
    Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
            .setMessage(getString(R.string.invitation_message))
            .setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
            .setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
            .setCallToActionText(getString(R.string.invitation_cta))
            .build();
    startActivityForResult(intent, REQUEST_INVITE);
}

Customize the invitation

When you build the invitation Intent, you must specify the title of the invitation dialog and the invitation message to send. You can also customize the image and deep link URL that get sent in the invitation, as in the example above, and you can specify HTML to send rich email invitations, which is recommended. See Firebase Invites: Best Practices.

MethodChannelsDescription
setMessage Email & SMS Sets the default message sent with invitations. This message can be edited by the sender in the invitation dialog. Cannot exceed 100 characters.
setDeepLink Email & SMS Sets the link into your app that is sent with invitations. Specify this to share specific content with the recipient or to otherwise present a custom experience when a user opens your app from an invitation.
setCustomImage Email Sets the URL of a custom image to include in email invitations. The image must be square and around 600x600 pixels. The image can be no larger than 4000x4000 pixels.
setCallToActionText Email Sets the call-to-action text of the button rendered in email invitations. Cannot exceed 32 characters.
setEmailHtmlContent Email Recommended: Sets the content of an email invitation. Set this to send rich HTML email invitations. Your HTML should include the placeholder string %%APPINVITE_LINK_PLACEHOLDER%%, which is replaced with the URL the recipient opens to accept the invitation. When you specify custom email messages, the setDescription, setCustomImage, and setCallToActionText methods have no effect.
setEmailSubject Email Required if setEmailHtmlContent is used. Sets the subject line of email invitations.

If you have an iOS version of your app and you want to send an invitation that can be opened on iOS in addition to Android, pass the OAuth 2.0 client ID of your iOS app to setOtherPlatformsTargetApplication when you build the app invitation intent. You can find your iOS app's client ID in the GoogleService-Info.plist file you downloaded from the Firebase console. For example:

Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
    ...
    .setOtherPlatformsTargetApplication(
        AppInviteInvitation.IntentBuilder.PlatformMode.PROJECT_PLATFORM_IOS,
        getString(IOS_APP_CLIENT_ID))
    ...
    .build();

Launching the AppInviteInvitation intent opens the contact chooser where the user selects the contacts to invite. Invites are sent via email or SMS. After the user chooses contacts and sends the invite, your app receives a callback to onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);

    if (requestCode == REQUEST_INVITE) {
        if (resultCode == RESULT_OK) {
            // Get the invitation IDs of all sent messages
            String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
            for (String id : ids) {
                Log.d(TAG, "onActivityResult: sent invitation " + id);
            }
        } else {
            // Sending failed or it was canceled, show failure message to the user
            // ...
        }
    }
}
    

Receive invitations

When a user receives an invitation, if the user has not yet installed the app, they can choose to install the app from the Google Play Store. Then, after the app is installed, or if the app was already installed, the app starts and receives the URL to its content, if you sent one. To receive the URL to your app's content, call the getDynamicLink method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    // Check for App Invite invitations and launch deep-link activity if possible.
    // Requires that an Activity is registered in AndroidManifest.xml to handle
    // deep-link URLs.
    FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
            .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                @Override
                public void onSuccess(PendingDynamicLinkData data) {
                    if (data == null) {
                        Log.d(TAG, "getInvitation: no data");
                        return;
                    }

                    // Get the deep link
                    Uri deepLink = data.getLink();

                    // Extract invite
                    FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(data);
                    if (invite != null) {
                        String invitationId = invite.getInvitationId();
                    }

                    // Handle the deep link
                    // ...
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "getDynamicLink:onFailure", e);
                }
            });
}
    

You must call getDynamicLink() in every activity that might be launched by the link, even though the link might be available from the intent using getIntent().getData(). Calling getDynamicLink() retrieves the link and invitation ID, and clears that data so it is only processed once by your app.

You normally call getDynamicLink() in the main activity as well as any activities launched by intent filters that match the link.

Send feedback about...

Need help? Visit our support page.