I'm trying to make it possible for people to send a link to one of their friends, I'm using the facebook API. The problem is that the send dialog doesn't show up, when I try to make a share dialog however it opens without any problem.
This is my code (it is set in a listadapter because in the list are different items that have the send_button (which is a normal button)):
CallbackManager callbackManager = CallbackManager.Factory.create();
final MessageDialog messageDialog = new MessageDialog((Activity)context);
messageDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e("test", "send success");
}
#Override
public void onCancel() {
Log.e("test", "send cancel");
}
#Override
public void onError(FacebookException e) {
Log.e("test", "send error");
}
});
send_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test","clicked");
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Test")
.setContentDescription("heytest")
.build();
messageDialog.show(linkContent);
}
});
The context is what is given through from the mainactivity. I don't get why it isn't working I've also overwritten the onActivityResult() method in the mainactivity.
I would appreciate some help, thanks.
Related
I want to ask the user of my android app the permission to publish on facebook when a custom share button (it's just an ImageView) is pressed. On button's OnClick method I execute this block:
CallbackManager facebookCallbackManager;
...
facebookCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
shareContent(activity, content, true);
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException error) { }
});
LoginManager.getInstance().logInWithPublishPermissions(activity, Collections.singletonList("publish_actions"));
And then I override:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
The problem is that the request never comes: an everlasting spinner wheel is presented and no callback (nor success, nor cancel, nor error) is called.
a. The user is already logged in, according to:
public static boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}
b. FacebookSdk.isInitialized() is true
c. Publish permissions are not granted, according to:
private static boolean hasPublishPermissions() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null && accessToken.getPermissions().contains("publish_actions");
}
d. Other uses of FB SDK are used through the app and they are functioning.
e. I am an app admin on FB dashboard
Any idea on the problem?
Important PS:
Since Facebook's API is extremely stable, depending on the time of the day or the position of the stars, without changing code I have three possible outcomes:
As described before, an everlasting spinner wheel.
The callback fires the onCancel method without the user interaction.
It shares the content without asking for confirmation - this gave me a nice unwanted video posted on my personal FB, without me noticing :) -
PS2:
Even the classic LoginManager.getInstance().logInWithReadPermissionnow is having the same issue. It never had it before.
then you can share content without everlasting spinner
LoginManager.getInstance().registerCallback(mCallbackManagerFacebook, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("Success");
if (loginResult.getRecentlyGrantedPermissions().contains("publish_actions")) {
new shareContent(activity, content, true);
} else {
//Toast.makeText(youractivity.this, "Successfully Connected to Facebook.", Toast.LENGTH_LONG).show();
GetFacebookPublishPermissions();
}
}
#Override
public void onCancel() {
//Log.d(TAG_CANCEL, "On cancel");
LoginManager.getInstance().logOut();
}
#Override
public void onError(FacebookException error) {
Toast.makeText(youractivity.this, "Looks like we are unable to connect to Facebook Servers.", Toast.LENGTH_LONG).show();
LoginManager.getInstance().logOut();
// Log.d(TAG_ERROR, error.toString());
}
});
and getFacebookSharepermission
public void GetFacebookPublishPermissions(){
if(LoginManager.getInstance()!=null)
{
LoginManager.getInstance().logInWithPublishPermissions(youractivity.this, Arrays.asList("publish_actions"));
}
}
Update
you can call this function like below
AccessToken token = com.facebook.AccessToken.getCurrentAccessToken();
if(token!=null)
{
if (token.getPermissions().contains("publish_actions")) {
new shareContent(activity, content, true);
}else{
GetFacebookPublishPermissions();
}
}else{
LoginManager.getInstance().logInWithReadPermissions(this,
Arrays.asList("public_profile", "email", "user_birthday"));
}
I think there is mistake for callback manager.
you declare and assign CallbackManager facebookCallbackManager;
facebookCallbackManager = CallbackManager.Factory.create();
Here you can see there is cbManager instead of facebookCallbackManager
so please double check for that.
V
LoginManager.getInstance().registerCallback(cbManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
shareContent(activity, content, true);
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException error) { }
});
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_register_rs1);
//String username = getIntent().getExtras().getString("username");
//Log.w("username is",username);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("FACEBOOK", "SUCCESS");
}
#Override
public void onCancel() {
// App code
Log.d("FACEBOOK", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FACEBOOK", "ERROR"+exception.toString());
}
});
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.d("FACEBOOK MANAGER", "SUCCESS");
}
#Override
public void onCancel() {
// App code
Log.d("FACEBOOK MANAGER", "CANCEL");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.d("FACEBOOK MANAGER", "ERROR");
}
});
i use this code to Login with facebook and when i press to the facebook login button .. it's just stay loading and the FacebookActivity never show..
this is my logcat screen:
W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 8328
I opened an issue with Facebook for this problem - or at least what I think is the same issue.. basically in my case the login process is very slow and seems to hang for some users/devices.
If anyone has a similar issue can you add additional details to the bug below so we can get someone at FB to investigate?
https://developers.facebook.com/bugs/264734493940722/
I have a problem. I want when I log in from my MainActivity to go to another activity, which I already did, but at the same time I don't want to be able to go back, and the logout button become in MainActivity2.
Code:
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
Intent mainLobby = new Intent (getActivity(), MainActivity2.class);
startActivity(mainLobby);
if(!isMainLobbyStarted) {
startActivity(mainLobby);
isMainLobbyStarted = true;
Toast.makeText(getActivity().getApplicationContext(), "Bem-vindo " + profile.getName(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
For you not to go back to activity 1, you can override the onBackPressed() on activity 2 to do nothing.
public void onBackPressed() {
//do nothing
}
Hope this helps.
I want to login Facebook SDK 4.3 with my own button but I'm facing some problems. Here is my code
public class MainActivity extends Activity {
CallbackManager callbackManager;
ShareDialog shareDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
Button bt= (Button)findViewById(R.id.button1);
shareDialog = new ShareDialog(this);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openFB();
}
});
}
public void openFB() {
//Login Callback registration
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "in LoginResult on success", Toast.LENGTH_LONG).show();
//Login success - process to Post
if (ShareDialog.canShow(ShareLinkContent.class)) {
String description = "description";
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("title")
.setContentDescription(description)
.setContentUrl(Uri.parse("http://google.com"))
.setImageUrl(Uri.parse("http://google.com"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
}
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "in LoginResult on cancel", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "in LoginResult on error", Toast.LENGTH_LONG).show();
}
});
LoginManager.getInstance().logInWithReadPermissions(this, "user_friends"); //Log in to FB
}
}
I get this error at the last line:
The method logInWithReadPermissions(Fragment, Collection) in
the type LoginManager is not applicable for the arguments
(MainActivity, String)
As you know there is another logInWithReadPermissions method with 2 parameters are (Activity,Collection) but I don't know I get this error. Please help me to fix this!
For someone who encounter the same problem with me, just change the last line like this:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); //Log in to FB
and it should be fine.
Here is my code:
public class LocationDetailActivity extends ActionBarActivity {
private CallbackManager mCallBackManager;
private FacebookCallback<LoginResult> mCallBack;
private ImageView mBtnBack;
AccessToken token;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_detail_layout);
FacebookSdk.sdkInitialize(getApplicationContext());
share = (ImageButton) findViewById(R.id.imageShare);
token = AccessToken.getCurrentAccessToken();
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(token == null) {
final Dialog dialog = new Dialog(LocationDetailActivity.this);
dialog.setContentView(R.layout.share_custom_dialog);
dialog.setTitle("Login as:");
dialog.show();
mCallBackManager = CallbackManager.Factory.create();
mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if(profile!=null){
dialog.cancel();
ShareDialog shareDialog = new ShareDialog(LocationDetailActivity.this);
if(shareDialog.canShow(ShareLinkContent.class)){
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
shareDialog.show(content);
}
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
LoginButton loginButton = (LoginButton)dialog.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(mCallBackManager, mCallBack);
}
else if(token != null){
ShareDialog shareDialog = new ShareDialog(LocationDetailActivity.this);
if(shareDialog.canShow(ShareLinkContent.class)){
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
shareDialog.show(content);
}
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallBackManager.onActivityResult(requestCode, resultCode, data);
}
}
I want to check if user already logged in from Facebook's app. If they didn't, my app will show a Custom Dialog with login button. Otherwise, my app will show Share Dialog, but when I click on my button, the Custom Dialog always be showed even I logged-in Facebook.
More info: AccessToken token = AccessToken.getCurrentAccessToken();
Check Login Status
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
Then you can later perform the actual login, such as in a custom button's OnClickListener:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
on a fast research, you must implement new methods to verify an active session on facebook SDK 4.0, like:
initialized your Facebok SDK (i guess you already do it)
Implement AccessTokenTracker
try to see this post, i guess it can help you