The Facebook SDK for Android enables people to sign into your app with Facebook Login. When people log into your app with Facebook they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf.
Generate your app signature for facebook
1)Open Command prompt
2)Fire Keytool command
keytool -exportcert -alias androiddebugkey -keystore "<path-to-users-directory>\.android\debug.keystore" | openssl sha1 -binary | openssl base64
KeyTool Path from file location
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\Your name\.android\debug.keystore" | openssl sha1 -binary | openssl base64
3)While you generating key hash it will ask for password you have to give password android
Create Application after generating kayhash
Create your new facebook application using below link
https://developers.facebook.com/apps
Add Gradle dependency to build.gradle in your android studio project
compile 'com.facebook.android:facebook-android-sdk:4.23.0'Add to your AndroidManifest.xml
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="your created facebook_app_id" />
<meta-data
android:name="com.facebook.sdk.AutoLogAppEventsEnabled"
android:value="false" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider1234"
android:exported="true" />
==> activity_facebook_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:gravity="center"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_facebook_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="@drawable/bg_btn_rounded_fb"
android:gravity="center">
<ImageView
android:id="@+id/iv_fb_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="@drawable/icon_facebook" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/iv_fb_logo"
android:layout_toRightOf="@+id/iv_fb_logo"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Invite Friend"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textSize="18dp" />
</LinearLayout>
==>FaceBookLoginActivity.java
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.share.model.AppInviteContent;
import com.facebook.share.widget.AppInviteDialog;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import butterknife.BindView;
import butterknife.ButterKnife;
public class FaceBookLoginActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.rl_facebook_invite)
RelativeLayout rl_facebook_invite;
@BindView(R.id.login_button)
LoginButton login_button;
String appLinkUrl, previewImageUrl;
CallbackManager callbackManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook_login);
ButterKnife.bind(this);
init();
printHashKey();
rl_facebook_invite.setOnClickListener(this);
}
public void printHashKey() {
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.yourpackage.name",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("Facebook KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
private void init() {
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
// if User want to logout from facebook than use following statement
// LoginManager.getInstance().logOut();
callbackManager = CallbackManager.Factory.create();
// Callback registration
login_button.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
}
});
LoginManager.getInstance().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
}
});
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.rl_facebook_invite) {
//Facebook App invites friends
appLinkUrl = "your app link url";
previewImageUrl = "preview image url";
// Uri targetUrl = AppLinks.getTargetUrlFromInboundIntent(SocialMediaLoginActivity.this, getIntent());
// if (targetUrl != null) {
// Log.i("Activity", "App Link Target URL: " + targetUrl.toString());
// }
if (AppInviteDialog.canShow()) {
AppInviteContent content = new AppInviteContent.Builder()
.setApplinkUrl(appLinkUrl)
.setPreviewImageUrl(previewImageUrl)
// .setPromotionDetails("Example Promotional Message", "EXAMPLEPROMO ")
.build();
AppInviteDialog.show(FaceBookLoginActivity.this, content);
}
}
}
}
No comments:
Post a Comment