Implementing Firebase Invites On Android

In this article, I am going to demonstrate how to implement Firebase Invites on Android. Firebase Invites is a solution for app referrals and sharing via email or SMS.

Firebase

Firebase is a back-end tool offered by Google for mobile and web applications. It offers Analytics, Cloud Messaging, Real-time Database, Storage, Remote Configuration, Notification, Crash Reporting, API’s, Multiple Authentication types, Hosting, Dynamic Links, and Invites. Google Firebase helps to develop the application rapidly.

Requirements

  • Android Studio.
  • Google Account.

Creating Project

Open Android Studio and create a new project.

Login to Google Firebase and create a new project in the console.

After creating the project, select “Add Firebase to your Android app” in Firebase Overview page.

After adding an application, give your Android application a package name, App nickname, debug signing certificate SHA-1, and hit the "REGISTER APP" button.

Now, download the Google-Services.json file and add it in the app folder of your application.

Adding Dependencies

We need to add dependencies for Google Services and Firebase Invites. 

Add the dependencies plug-in to App Gradle for Google Firebase Invites.

  1. compile 'com.google.firebase:firebase-invites:10.0.1'  
Add Gradle plug-in (build.gradle(app)) at the end of the code.
  1. apply plugin: 'com.google.gms.google-services'  
Add classpath plug-in to the project gradle. 
  1. classpath 'com.google.gms:google-services:3.0.0'  

Creating Layout

Now, go to the activity_main.xml and paste the below code. 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="io.github.saravanan_selvaraju.firebaseinvites.MainActivity">  
  9.     <TextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:gravity="center"  
  13.         android:text="@string/msg_txt"  
  14.         android:textSize="28dp"  
  15.         android:padding="10dp"  
  16.         android:layout_margin="10dp"/>  
  17.     <LinearLayout  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:orientation="vertical"  
  21.         android:layout_gravity="center"  
  22.         android:layout_margin="15dp"  
  23.         android:padding="15dp">  
  24.         <Button  
  25.             android:layout_width="match_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:id="@+id/invie_btn"  
  28.             android:background="@color/colorPrimary"  
  29.             android:text="@string/invite_txt"  
  30.             android:textColor="@android:color/white"  
  31.             android:textStyle="bold"/>  
  32.   
  33.     </LinearLayout>  
  34.   
  35. </LinearLayout>  

Creating Activity

Write the below code inside “button onclick listener()” method for sending invitations.

  1. private void onInviteClicked() {  
  2. Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))  
  3.             .setMessage(getString(R.string.invitation_message))  
  4.             .build();  
  5.     startActivityForResult(intent, REQUEST_INVITE);  
  6. }  
Add String Resources in string.xml file.
  1. <resources>      
  2.     <string name="invitation_title">Firebase Invite</string>  
  3.     <string name="invitation_message">Hey there, I found a app....</string>  
  4. </resources>  
This onActivityResult() method calls back the MainActivity after the user sends an invite.
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  2.     super.onActivityResult(requestCode, resultCode, data);  
  3.     Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);  
  4.   
  5.     if (requestCode == REQUEST_INVITE) {  
  6.         if (resultCode == RESULT_OK) {  
  7.             String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);  
  8.             for (String id : ids) {  
  9.                 Log.d(TAG, "onActivityResult: sent invitation " + id);  
  10.             }  
  11.         } else {  
  12.         }  
  13.     }  
  14. }  
MainActivity.java
  1. package io.github.saravanan_selvaraju.firebaseinvites;  
  2.   
  3. import android.content.Intent;  
  4. import android.net.Uri;  
  5. import android.support.annotation.NonNull;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11.   
  12. import com.google.android.gms.appinvite.AppInvite;  
  13. import com.google.android.gms.appinvite.AppInviteInvitation;  
  14. import com.google.android.gms.common.ConnectionResult;  
  15. import com.google.android.gms.common.api.GoogleApiClient;  
  16.   
  17. public class MainActivity extends AppCompatActivity {  
  18.     private static final int REQUEST_INVITE =100 ;  
  19.     private static final String TAG =MainActivity.class.getSimpleName() ;  
  20.     private Button Invite_btn;  
  21.     @Override  
  22.     protected void onCreate (Bundle savedInstanceState){  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         Invite_btn = (Button) findViewById(R.id.invie_btn);  
  26.   
  27.         Invite_btn.setOnClickListener(new View.OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View view) {  
  30.                 onInviteClicked();  
  31.             }  
  32.         });  
  33.     }  
  34.     private void onInviteClicked() {  
  35.         Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))  
  36.                 .setMessage(getString(R.string.invitation_message))  
  37.                 .build();  
  38.         startActivityForResult(intent, REQUEST_INVITE);  
  39.     }  
  40.     @Override  
  41.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  42.         super.onActivityResult(requestCode, resultCode, data);  
  43.         Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);  
  44.   
  45.         if (requestCode == REQUEST_INVITE) {  
  46.             if (resultCode == RESULT_OK) {  
  47.                 String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);  
  48.                 for (String id : ids) {  
  49.                     Log.d(TAG, "onActivityResult: sent invitation " + id);  
  50.                 }  
  51.             } else {  
  52.             }  
  53.         }  
  54.     }  
  55.   
  56. }  

Adding Permission

Don’t forget to add internet access permissions in androidmanifest.xml.
  1. uses-permission android:name="android.permission.INTERNET"/>  
         

Summary

In this article, we discussed what Google Firebase is, and how to use Firebase Invites in Android applications.

Up Next
    Ebook Download
    View all
    Learn
    View all