Categories
android

App Invites in Android – Using Google Services

By leveraging the power of Google Services, we can easily send invites to our friends to start using an app we really like. No need to implement custom solutions as this takes care of the typical use case for getting more people into your app. Best of all? It is really simple to implement!

How to implement Google App Invites in your Android App

  1. Add the app invites dependency to your project:
    1. Add the following to your AndroidManifest.xml
      <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    2. Add the Google Services plugin to your projects build.gradle file
      classpath 'com.google.gms:google-services:1.5.0-beta2'
    3. Apply the plugin in your app level build.gradle (normally app/build.gradle)
      apply plugin: 'com.google.gms.google-services'
    4. Add the dependency to your app level build.gradle:
        compile "com.google.android.gms:play-services-appinvite:8.3.0"
      
  2. Get a google-services.json  file generated for your project. You can get a configuration file here. This will create a JSON file after you have entered in your app’s information. Place this file in the app/ folder of your project. It is also advisable to add this file to .gitignore as you might not want this information shared. This JSON file is needed by the Google Services plugin in order to know what services need to be configured.
  3. In your Activity, on a button click create an intent to send an invite.
     @Override
        public void sendShareEvent(String bookTitle, String bookImageUrl, String bookDeepLinkUrl) {
            try {
                Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
                        .setMessage(getString(R.string.invitation_message, bookTitle))
                        .setDeepLink(Uri.parse(bookDeepLinkUrl))
                        .setCustomImage(Uri.parse(bookImageUrl))
                        .setCallToActionText(getString(R.string.invitation_cta))
                        .build();
                startActivityForResult(intent, INVITE_REQUEST_CODE);
            } catch (ActivityNotFoundException ac) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.sharing_book_title, bookTitle));
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        }
        
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == INVITE_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
                    Log.d(TAG, getString(R.string.sent_invitations_fmt, ids.length));
                } else {
                 
                    Log.d(TAG, "invite send failed or cancelled:" + requestCode + ",resultCode:" + resultCode );
                }
            }
        }
    • Make sure your invite message is less than 100 characters.
    • You can add custom deep links that the user will be sent to if the app is already installed.
    • You can also track the result of the invite and apply certain discounts for the user if an invite was opened or the app was installed from it. Neat!
    • Make sure you are testing on an emulator that is using Google APIs. Some emulators (like Genymotion) & real devices won’t have Google Services installed. This could result in an exception being thrown when trying to start the intent (ActivityNotFoundException). You should also cater for this and do something else. In this example, I am sharing plain text with a link to the app to download.
    • If you are getting the “Failed to start” error when trying to launch the activity, make sure that the SHA-1 that you entered when configuration Google Services is correct. You might be building a debug version when you entered in your release SHA-1 when configuring your app. [More info]
  4. Run the app and you will see the new invite system! You will see a list of suggested contacts from Google, which includes the people you contact most frequently. You will be able to send to email addresses and as SMSs.  You can customise the text and add an image if you want.Google App Invites in action App Invite Sent example
  5. This is what an email will look like, clicking on the link will take the user to the play store or to the app if the app is installed. (Once the app is installed and you have configured a deep link, the app can then open up that deep link from the invite).App Invites - Email example invite
  6. If you want to know whether a user opened your app from an invite that was sent, do the following in your activity:
      private void checkIfComingFromInvite(){
            googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(AppInvite.API)
                    .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                        @Override
                        public void onConnectionFailed(ConnectionResult connectionResult) {
                            Log.d(TAG, "onConnectionFailed: onResult:" + connectionResult.toString());
    
                        }
                    })
                    .build();
            AppInvite.AppInviteApi.getInvitation(googleApiClient, this, true)
                    .setResultCallback(
                            new ResultCallback<AppInviteInvitationResult>() {
                                @Override
                                public void onResult(AppInviteInvitationResult result) {
                                    Log.d(TAG, "getInvitation:onResult:" + result.getStatus());
                                }
                            });
        }

    This will automatically follow a deep link that you may have attached on the invite. For more information about deep links read here.

    You can see this code in action in my open source app Book Dash develop branch, alternatively check out the sample from google here.

App Invites are also available for iOS too.

What other use cases can you find for this new invite system?

One reply on “App Invites in Android – Using Google Services”

Comments are closed.