Increase user engagement on mobile app using facebook

Integrating facebook SDK not only helps to drive user engagement on mobile app but also enhances user registration and sign-in process. Users no longer need to fill in yet another registration form or remember another username and password to use our app. Using Facebook for login provides us with all the information we need to create a social, personalize experience from the moment the user visits our app. To drive more traffic to our app; Facebook enable various channels that enable new users to discover our app as well as existing users to re-engage with our app. So these advantages increase the app quality.  In this post, I am going to discuss about integrating Facebook Login and Logout  in Android applications using Facebook SDK 4.0.

What is Facebook SDK 4.0?

Facebook provides SDK for Android to build Android applications to increase the intraction with it’s World’s number one Social Networking platform.The Facebook SDK 4.0 for Android is the easiest way to integrate Facebook in android app. It has the following functionalities:

  • Facebook login by enabling authentication with Facebook credentials.
  • It enables to log events in our android application.
  • People can send  and share content from our android app to Facebook.
  • It also enables read and write to Graph API.

Integrate facebook SDK to increase user engagement on mobile app

  • Add this to Module-level /app/build.gradle before dependencies:

 

  • Add the compile dependency  of the Facebook SDK in the build.gradle file:
    dependencies { 
      compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    }

     

  • Build your project.
  • We need to initialize Facebook SDK before we can use it. Add the below call from onCreate in Activity or Application:
    @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         FacebookSdk.sdkInitialize(getApplicationContext());
    }

     

  • Generate your Facebook App ID and store it into project’s strings file.

How to register your app with Facebook to get Facebook App Id .

<string name="facebook_app_id">000001111122222</string>
  • Update the Android manifest by adding Facebook App ID in meta-data and some permissions.
    <uses-permission android_name="android.permission.INTERNET"/>
    
    <application android_label="@string/app_name" ...>
        ...
        <meta-data android_name="com.facebook.sdk.ApplicationId" android_value="@string/facebook_app_id"/>
        ...
    </application>

     

  • To use Facebook Login, also add the FacebookActivity to the Android manifest:
    <activity android_name="com.facebook.FacebookActivity"
              android_configChanges=
                     "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
              android_label="@string/app_name" />
  • Create a Hash Key for development

To authenticate interactions between our app and the Facebook app,Facebook uses the key hash . If we use  Facebook Login, we need to add our Android development key hash to your Facebook developer profile.For help read this document.

Steps for Facebook Login for Android

  • Add Facebook Login button into our layout XML file :
    <com.facebook.login.widget.LoginButton
        android_id="@+id/login_button"
        android_layout_width="wrap_content"
        android_layout_height="wrap_content"
        android_layout_gravity="center_horizontal" />

     

  • Register a Callback in the application.We can register Callback either with Login button or LoginManager for e.g
    CallbackManager callbackManager;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
    
        callbackManager = CallbackManager.Factory.create();
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                    }
    
                    @Override
                    public void onCancel() {
                         // App code
                    }
    
                    @Override
                    public void onError(FacebookException exception) {
                         // App code   
                    }
        });
        }

     

  • Perform the actual login :
     LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile ", "email",
                    "user_birthday"));
    

     

  • Finally, in the onActivityResult in our Activity, pass the result to the CallbackManager:
    @Override
     protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
  • Facebook Logout from application:

To logout from the application using Facebook SDK is very easy .We need to include only one line in our android application.

LoginManager.getInstance().logOut();

Conclusion

Facebook SDK 4.0 has came with many new features than before,such as Access Token, LoginManager and CallBackManager classes supercede and replace functionality in the Session class .It is easy to implement  android with  Facebook sdk 4.0.This blog has explained the Facebook Login and Logout in our android application.

Refrerences

Facebook SDK 4.0

Facebook integeration in Android

Leave a Comment

Scroll to Top