How to use android Firebase Invites in application

    0
    97

    Android Firebase Invites are the way different solution to app referrals and sharing with help of email or SMS.In this example, we will learn how to use android Firebase Invites on Android.

    Download Project – FirebaseInvitesDemo

    Download Apk – Apk

    Wants to Learn Advanced Android Application development with Firebase – Beyond Basics

    Creating New Project – FirebaseInvitesdemo

    Open your Android Studio & create a new Project, have taken Blank Activity for this project and clicked finish.

    Pre-Requisites

    To add Firebase on your project

    • Go to Firebase Console and Log in with your Google account.
    • Click on Create Project. Enter your project name and country.
    • Click on Add Firebase on your Android app and specify your package name.
    • Download json-config file and paste it to the app folder in your project.

    To use Firebase we have to add classpath dependency to the project level build.gradle file and apply the plugin to the top of the application level build.gradle file and also add a dependency for firebase invites.

    apply plugin: 'com.google.gms.google-services'
     compile 'com.google.firebase:firebase-invites:9.2.0'
      classpath 'com.google.gms:google-services:3.0.0'

    Add the above code and sync the project.

    Creating Layout

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.rubs.firebaseinvitesdemo.MainActivity">
    
    
        <Button
            android:id="@+id/invite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="213dp"
            android:text="Invite Friends" />
    </RelativeLayout>
    

    This XML file contains one button – Invites Friends.

    Related:

    Upload image from gallery to firebase in Android

    Periodically send GPS location to Firebase with custom time frame in Android

    Display current location(latitude and longitude) in google map and save it to the Firebase database in Android

    Android Firebase working with Realtime Database and Storage

    Creating MainActivity.java

    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;
    
    import com.google.android.gms.appinvite.AppInviteInvitation;
    
    public class MainActivity extends AppCompatActivity {
        Button invite;
        private static final  int REQUEST_INVITE = 0;
        private static final String TAG = MainActivity.class.getSimpleName();
        @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){
                    String[] ok = AppInviteInvitation.getInvitationIds(requestCode, data);
                    for(String id : ok){
                        Log.d(TAG, "onActivityResult: sent invitation" +id);
                    }
                }else {
                    Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
                }
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            invite = (Button)findViewById(R.id.invite);
    
            invite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new AppInviteInvitation.IntentBuilder("Send app invitation")
                            .setMessage("This is my app")
                            .setDeepLink(Uri.parse("http://example.com"))
                            .build();
                    startActivityForResult(intent, REQUEST_INVITE);
                }
            });
    
        }
    }

    When the user clicks on the button, it launches an Activity which will list all contacts to send the invitation. We have put initial configuration which includes a message, deep link. When the user selects and sends invites to the contacts, It will return invitation ids of all invites in onActivityResult() method.

     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){
                    String[] ok = AppInviteInvitation.getInvitationIds(requestCode, data);
                    for(String id : ok){
                        Log.d(TAG, "onActivityResult: sent invitation" +id);
                    }
                }else {
                    Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
                }
    

    By using

     String[] ok = AppInviteInvitation.getInvitationIds(requestCode, data);

    we get the invitation ids of all sent message.

    when

     for(String id : ok)

    it will send an invitation.

    Conclusion
    In this Project, you saw how to use android Firebase Invites. In the case of any queries, you may ask questions. Keep following for more amazing Android Blogs. If You are Beginner Learn Android application development from the scratch

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here