The Effortless Way to Integrate Facebook Login and Grow Your Android App’s Registration by 40%

0
Shares
The Effortless Way to Integrate Facebook Login and Grow Your Android App’s Registration by 40%
4 (80%) 1 vote

You know that Android is an open-source platform. This means you can easily contribute to the Android community by developing apps.

Objective:

How to integrate facebook login in your native Android app.

The recent study revealed that three out of four people leave a long registration process. This situation is even more critical in the mobile app world. Because, a mobile user with the on-screen keyboard is most likely to leave an app, rather than troubling him/herself of going through the registration process. That is why the Android apps with Facebook login are the winners.

Let’s get started.

STEPS:

#1: Create a New Project

Go to File menu in your Android Studio, click on new and you’ll see an option of the project, click on it.

#2: Get Key Hash Value

After you add facebook library, you’ll need to get key hash value. So, in order to get this value, write the below code in ‘oncreate()’ method of ‘Mainactivity.java’.

try {
PackageInfo info = getPackageManager().getPackageInfo(
getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

#3: Import Gradle

Next, you’ll need to import Facebook Android SDK grade shown as below.

compile ‘com.facebook.android:facebook-android-sdk:4.+’

#4: Create Facebook App ID

To do this, go to facebook developer site (https://developers.facebook.com/).

After developer facebook login, click on the ‘Add a New App’.

1

Now, enter your details and click on ‘Create App ID’.

2

Once you create your app, go to application dashboard and copy the ‘App ID’.

3

After you get your App ID, it’s time to register package and activity. Go to the settings from left-side menu and click on ‘Add Platform’.

4

Now, select ‘Android platform’.

5

Here, register your application package name and activity that use facebook sharing. And, add the key hash value that we explained in Step: 2.

6

#5: Add App ID

Add the App ID in the Strings.xml file of your application.

<string name="app_id">your app_id</string>

#6: Add below code to AndroidManifest.xml

<activity
android:n<uses-permission android:name="android.permission.INTERNET">ame="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />

#7: Code to integrate Facebook Login

CallbackManager callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email", "user_birthday"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {

@Override
public void onSuccess(final LoginResult loginResult)
{

final String accessToken = loginResult.getAccessToken().getToken();
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
Log.e("RESPONSE:-",object.toString());
Log.e("GRAPH_RESPONSE:-",response.toString());

}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,gender,email,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception)
{
Log.e("ERROR", exception.toString());
}
});
}

#8: Override below

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

So, by implementing the above code, you can add a ‘sign in with Facebook’ button in your Android application.

You can find a copy of the same code on GitHub here.

Nowadays, most companies including Big enterprises and Startups always prefer to integrate Facebook login in their registration pages. So, if you too want to grow your app audience and registration rates, you can simply hire Space-O Technologies, which is a leading Mobile App Development Company and we have worked with world’s finest startups to turn their ideas into an App. You can check our portfolio here for more details.

 
0
Shares
 

LET'S TALK VALIDATE YOUR IDEA!