Sunday, August 14, 2016

Social Integration (FaceBook) in your App- Part I


Hi .... Dear All ....  Today I am uploading a superb example of how Social Integration can be done with you App.
I am integrating the world most popular social site/app @ FaceBook.
In lot of app you have seen on the login page "Login with Facebook " . This functionality is basically used to skip the Registration Procedure so user can enter in your app by using another account like FB.
There are lot of steps please read carefully otherwise you will not reach at your destination.
@  Register yourself as a developer on     https://developers.facebook.com/apps
@ After Registration Add a New App  by clickin on Green Button
@Select Android and Write your App Name go for Create App ID
@ Write your Credentials and Select Options i had selected for Education purpose
@ After Successful generation of App ID  you can goto Quick Start Section or follow my steps .
 Now
1. In your project build.gradle file please add
repositories {
        mavenCentral()
    }
 2. Add compile 'com.facebook.android:facebook-android-sdk:[4,5)' to        
      your build.gradle dependencies.
3. Build Project import package in your Activity as import com.facebook.FacebookSdk;

4. Add in string.xml file as <string name="facebook_app_id">29512.........</string>
5. Add metadata in manifest file in application element as


    ...
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    ...
6. Tell Facebook about your project means package name and Launcher Activity name.
@@@ Don't worry about package name alert dialog if you are not going to upload your App to google play.
7. Generate Hashkey  
7.1    On Mac use the command
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
7.2 On Windows use 

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
***** In most cases people found an openssl error so please first download openssl from 
https://code.google.com/archive/p/openssl-for-windows/downloads
***** Extract zipfile in C drive in your any folder like i created OpenSSL and extract zip  here
Now in command window copy any one command from 7.1 or 7.2 given above and replace 
openssl   by "C:\OpenSSL\bin\openssl.exe"   like for windows as 
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | "C:\OpenSSL\bin\openssl.exe"  sha1 -binary | "C:\OpenSSL\bin\openssl.exe"  base64
******* Insert password android  
***** see your HashgKey copy it and paste onto facebook developer page ... you will get finished message.
********** Congratulations you had integrated FB in your App*****************

In this example you can set alarm after a specified time interval in secs. After your specified time interval your device will vibrate.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box
Now register  FB Activity in your project  manifest file like

<activity android:name="com.facebook.FacebookActivity"

    android:configChanges=

        "keyboard|keyboardHidden|screenLayout|screenSize|orientation"

    android:theme="@android:style/Theme.Translucent.NoTitleBar"

android:label="@string/app_name" />

Add Internet permission
<uses-permission android:name="android.permission.INTERNET"/>

1. Layout File is as
                         


2. XML file is as  with FB Login Button
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.exam.ravi.socialintegration.MainActivity"   >

    <com.facebook.login.widget.LoginButton

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/loginFB"

        android:layout_centerVertical="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true" />

    <TextView        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="New Text"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true" />

</RelativeLayout>

3. Java Code is as MainActivity.java

package com.exam.ravi.socialintegration;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends AppCompatActivity {
    TextView textView;
    LoginButton loginButton;
    CallbackManager callbackManager;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        callbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton) findViewById(R.id.loginFB);
        textView = (TextView) findViewById(R.id.textView);
        loginButton.registerCallback(callbackManager, 
                   new FacebookCallback<LoginResult>() {
            @Override

            public void onSuccess(LoginResult loginResult) {
            
            textView.setText("User Id= " + loginResult.getAccessToken().getUserId());

            }

            @Override            public void onCancel() {
                Log.v("Ravi", "User Cancelled");
            }

            @Override            public void onError(FacebookException error) {
                Log.v("Ravi", "Something Wrong");
            }
        });
    }
        @Override

   protected void onActivityResult( int req, int res, Intent intnt)
        {
            callbackManager.onActivityResult(req,res,intnt);
        }

}

No comments:

Post a Comment