Implementing Firebase Invites On Android

Introduction

 
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 a 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 the 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. import android.content.Intent;  
  3. import android.net.Uri;  
  4. import android.support.annotation.NonNull;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import com.google.android.gms.appinvite.AppInvite;  
  11. import com.google.android.gms.appinvite.AppInviteInvitation;  
  12. import com.google.android.gms.common.ConnectionResult;  
  13. import com.google.android.gms.common.api.GoogleApiClient;  
  14. public class MainActivity extends AppCompatActivity {  
  15.  private static final int REQUEST_INVITE = 100;  
  16.  private static final String TAG = MainActivity.class.getSimpleName();  
  17.  private Button Invite_btn;  
  18.  @Override  
  19.  protected void onCreate(Bundle savedInstanceState) {  
  20.   super.onCreate(savedInstanceState);  
  21.   setContentView(R.layout.activity_main);  
  22.   Invite_btn = (Button) findViewById(R.id.invie_btn);  
  23.   Invite_btn.setOnClickListener(new View.OnClickListener() {  
  24.    @Override  
  25.    public void onClick(View view) {  
  26.     onInviteClicked();  
  27.    }  
  28.   });  
  29.  }  
  30.  private void onInviteClicked() {  
  31.   Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title)).setMessage(getString(R.string.invitation_message)).build();  
  32.   startActivityForResult(intent, REQUEST_INVITE);  
  33.  }  
  34.  @Override  
  35.  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  36.   super.onActivityResult(requestCode, resultCode, data);  
  37.   Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);  
  38.   if (requestCode == REQUEST_INVITE) {  
  39.    if (resultCode == RESULT_OK) {  
  40.     String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);  
  41.     for (String id: ids) {  
  42.      Log.d(TAG, "onActivityResult: sent invitation " + id);  
  43.     }  
  44.    } else {}  
  45.   }  
  46.  }  

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.


Similar Articles