Before start testing invites you should:
- Connect your app to your Firebase project, do so from the Firebase console.
- Enable Firebase Dynamic Links, do so from the Firebase console by opening the Dynamic Links section and accepting the terms of service if prompted.
- Add Firebase to your Android project.
- Add the dependency for Firebase Invites to your app-level
build.gradle
file:
Gradle file:
compile 'com.google.firebase:firebase-invites:9.0.2'
Send Invitations
Build 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);
}
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 getInvitation
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// Create an auto-managed GoogleApiClient with access to App Invites.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(AppInvite.API)
.enableAutoManage(this, this)
.build();
// 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.
boolean autoLaunchDeepLink = true;
AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
.setResultCallback(
new ResultCallback<AppInviteInvitationResult>() {
@Override
public void onResult(AppInviteInvitationResult result) {
Log.d(TAG, "getInvitation:onResult:" + result.getStatus());
if (result.getStatus().isSuccess()) {
// Extract information from the intent
Intent intent = result.getInvitationIntent();
String deepLink = AppInviteReferral.getDeepLink(intent);
String invitationId = AppInviteReferral.getInvitationId(intent);
// Because autoLaunchDeepLink = true we don't have to do anything
// here, but we could set that to false and manually choose
// an Activity to launch to handle the deep link here.
// ...
}
}
});
}
IMPORTANT: Code above requires a connected GoogleApiClient with AppInvite.API
enabled.
If the launchDeepLink
parameter is true
, the app automatically relaunches with the URL to your app's content, which your app can handle normally. If the launchDeepLink
parameter is false
, you can manually start the intent returned by getInvitationIntent to handle the URL when appropriate.
Here is more info about hot to Send and Receive Firebase Invites from Your Android App.
Link Testing in Android Studio
Also you can Use the Deep Link Testing feature for Android Studio version 2.x feature to verify that your app can be launched with a specified URL. To set this up, first select Run > Edit Configurations from the Android Application > General section. To test a HTTP URL, select Deep link in the Launch Options, and then input the URL to test. If the link is successful, the app should launch in the emulator or on the connected device. Otherwise, an error message appears in the Run window.
Android Debug Bridge
Test that your links open your app by using the Android Debug Bridge, where {URL}
represents the HTTP URL declared in your app manifest.
adb shell am start -a android.intent.action.VIEW -d "{URL}" com.example.android
At link there is more info about how to test your implementation.