Hi I have problem with Facebook:
Case:
1.User has no Facebook app.
2.User logins into Facebook via WebDialog
3.User gives all permissions for share, and shares post
4.User enters Facebook account, than into applications, and removes my app.
5.User tries to make share again.
6."Unknown error. Please try again later" Appears in WebDialog.
Is there a way to fix this case?
I found that using ShareDialog i can avoid this problem when user has facebook app installed, but I don't know how to solve it if user has no fb app on his phone.
To show dialog I verify:
private boolean checkFacebookLogin(){
Session session = Session.getActiveSession();
if(session!=null && session.isOpened() ){
return true;
}
return false;
}
Than i ask for permissions if they are needed:
private void performPublish() {
Session session = Session.getActiveSession();
pendingAction = PendingAction.POST_STATUS_UPDATE;
if (session != null && mCurrentActivity!=null) {
if (hasPublishPermission()) {
// We can do the action right away.
handlePendingAction();
} else {
// We need to get new permissions, then complete the action when we get called back.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(mCurrentActivity, PERMISSIONS));
}
}
}
In the end i show WebDialog:
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(mCurrentActivity,
Session.getActiveSession(),
postParams))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
}
})
.build();
feedDialog.show();
After showing WebDialog, it redirects to error page with "Unknow error [...]" text, i have found no error information, so I don't even know that something goes wrong.
I tried HelloFacebookSample, but there if user has no facebook app, he can't edit post in facebook dialog. I want to see Facebook dialog in both cases ( with/without fb app installed).
if (FacebookDialog.canPresentShareDialog(this,
FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
this)
.setLink(// what ever you want to share use here
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
Log.d("Tag", "Success!");
publishFeedDialog();
} else {
//ask the user to login .
//authButton.performClick();
share = true;
// }
}
So from the above code if the fb app is already installed it will open that app else you have to ask the user to login by performing Fb LoginButton . performClick(). so the user will be redirected to web dialog of fb login. the onLogin success call back u can share using.,
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("link",
"");
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
MenuActivity.this, Session.getActiveSession(), params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(MenuActivity.this, "Posted",
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(
MenuActivity.this
.getApplicationContext(),
"Publish cancelled", Toast.LENGTH_SHORT)
.show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(
MenuActivity.this.getApplicationContext(),
"Publish cancelled", Toast.LENGTH_SHORT)
.show();
} else {
// Generic, ex: network error
Toast.makeText(
MenuActivity.this.getApplicationContext(),
"Error posting story", Toast.LENGTH_SHORT)
.show();
}
}
}).build();
feedDialog.show();
}
Related
As per this link here, I have installed the SDK 3.2.1 and implemented a uiHelper, along with FacebookDialog like this:
if (FacebookDialog.canPresentShareDialog(getApplicationContext(),
FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
// Publish the post using the Share Dialog
Toast.makeText(this, "if", Toast.LENGTH_LONG).show();
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
this).setLink("https://developers.facebook.com/android")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
Toast.makeText(this, "else", Toast.LENGTH_LONG).show();
// Fallback. For example, publish the post using the Feed Dialog
publishFeedDialog();
}
For publishFeedDialog() I have:
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("name", "Facebook SDK for Android");
params.putString("caption",
"Build great social apps and get more installs.");
params.putString(
"description",
"The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture",
"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(this,
Session.getActiveSession(), params)).setOnCompleteListener(
new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(getApplicationContext(),
"Posted story, id: " + postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(getApplicationContext(),
"Publish cancelled", Toast.LENGTH_SHORT)
.show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(getApplicationContext(),
"Publish cancelled", Toast.LENGTH_SHORT)
.show();
} else {
// Generic, ex: network error
Toast.makeText(getApplicationContext(),
"Error posting story", Toast.LENGTH_SHORT)
.show();
}
}
}).build();
feedDialog.show();
}
Now the FacebookDialog.canPresentShareDialog is always going to false even when Fb app is installed in the phone and then the publishFeedDialog() is called. But the app crashes after this.
What is the solution for this? Why is the normal facebook dialog shown when the app is there in my phone? (I am using Lenevo model, if that helps)
You need a newer FB app in order for the share dialog to work. Make sure you download the latest ones!
Also, no, you don't need login to enable the share dialog.
Im trying to use facebook request dialog to enable the user to pick a friend to challenge in a turn based trivia android game.
i dont have a web version of the game so i dont canvas.
ive set up a request dialog that shows the list friends which works fine when i log in with facebook test users but when i login with my user who is also administrator of the facebook app then the dialog shows a list of only 5 friends. the dialog allows to search for more friends and when i do that i can search for other friends which do not appear in the list.
my user has "public_profile","read_friendlists", "user_friends" permissions.
the request dialog code
private void sendRequestDialog() {
Bundle params = new Bundle();
params.putString("message", "Learn how to make your Android apps social");
params.putString("filters", "app_non_users");
WebDialog requestsDialog = (
new WebDialog.RequestsDialogBuilder(MainActivity.this,
//getActivity(),
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error != null) {
if (error instanceof FacebookOperationCanceledException) {
Toast.makeText(getApplicationContext(),
//getActivity().getApplicationContext(),
"Request cancelled",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
//getActivity().getApplicationContext(),
"Network Error",
Toast.LENGTH_SHORT).show();
}
} else {
final String requestId = values.getString("request");
if (requestId != null) {
Toast.makeText(getApplicationContext(),
//getActivity().getApplicationContext(),
"Request sent",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
//getActivity().getApplicationContext(),
"Request cancelled",
Toast.LENGTH_SHORT).show();
}
}
}
})
.build();
requestsDialog.show();
}
i dont understand why its not showing the entire friends list. please help.
in addition, is this method of challenging a friend for a game is permitted by facebook is there another more recommended method?
Thanks!
user_friends will return only the friends who used the app that makes the request. via Graph API Reference
I'm trying to share some stuff on Facebook. following this guide I managed to complete the sharing when you have the native app. However, when I reached this:
Step 4: Using the Feed Dialog as fallback. It says that to be able to use a fallback method, I need to add the code below
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("name", "Facebook SDK for Android");
params.putString("caption", "Build great social apps and get more installs.");
params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(getActivity(),
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(getActivity(),
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(getActivity().getApplicationContext(),
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
After adding this, I get errors at getActivity(), I fix this by using MyClass.this.
Then, when I run the app (I directly run this method at startup) my application crashes and I get some error like "attempted to use session that was not open"
Anyone know how to be able to share stuff without the native app installed?
The error is caused because the user has not logged in to Facebook via your app. You will need to implement the Login with Facebook somewhere in your app. Even if the user does not have the native app installed, the Facebook SDK will fall back on the webdialog to request the user to log in. Thus, You will need to check if the session is opened (i.e. user has logged in).
Session session = Session.getActiveSession();
boolean isSessionOpen = (session != null && session.isOpened());
if(!isSessionOpen) {
Toast.makeText(getApplicationContext(),"Please Login With Facebook First!", Toast.LENGTH_SHORT).show();
return;
}
Bundle params = new Bundle();
params.putString("to", useriId);
params.putString("message","Hello World");
WebDialog requestsDialog = (new WebDialog.RequestsDialogBuilder(
JoinVia.this, Session.getActiveSession(), params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error != null) {
if (error instanceof FacebookOperationCanceledException) {
Toast.makeText(
JoinVia.this.getApplicationContext(),
"Request cancelled", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
JoinVia.this.getApplicationContext(),
"Network Error", Toast.LENGTH_SHORT)
.show();
}
} else {
final String requestId = values
.getString("request");
if (requestId != null) {
Toast.makeText(
JoinVia.this.getApplicationContext(),
"Request sent", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
JoinVia.this.getApplicationContext(),
"Request cancelled", Toast.LENGTH_SHORT)
.show();
}
}
}
}).build();
requestsDialog.show();
This my code to send invitation to friends id. The code can send the notification, but when i click the notification icon from the browser, the invitation is not present. Can anyone guide me how to send a message to friends facebook.
UPDATED:
Ignore my previous answer, I misinterpreted the type of dialog that you were trying to produce.
As mentioned in the comments, your code doesn't seem to be the problem since you are successfully getting a notification. Simply follow the steps in the Facebook app creation doc to make sure you are setting your app up correctly, specifying a Canvas URL as shown in the first image to make sure your app can provide invitations properly.
The examples on the same page should provide more help with using the URLs also.
I have tried the tutorial http://developers.facebook.com/docs/mobile/android/build/#sdk and I am able to login successfully. this code provides a dialog to login. But I want to make this as an Activity . Because I have to perform other tasks too from this activity. Like when new activity starts after sucessful login come back to previous (facebook) activity again in previous state of facebook. Please help me.
Thanks
The dialog is only created for the user to log into Facebook. Once they have done that it will return back to the original activity. This is what you are asking for right?
Lets say you want to post a message to facebook.
try {
Log.d(TAG, "postToFaceBook()");
if (facebook == null) {
facebook = new Facebook(API);
String access_token = prefs.getFBAccesTocken();
long expires = prefs.getFBExpiry();
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
}
if (facebook.isSessionValid()) {
Log.d(TAG, "Session is valid");
facebook.extendAccessTokenIfNeeded(this, null);
postToFacebook();
} else {
Log.d(TAG, "not valid");
// Using SSO OAuth
// facebook.authorize(this, new String[] { "publish_stream"
// },new LoginDialogListener());
// Not using SSO
facebook.authorize(this, new String[] { "publish_stream" },
Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
} catch (NullPointerException e) {
Log.e(TAG, "An error occurd trying to open facebook app");
If the user has logged onto facebook in the past and has a valid session, it will simply post to Facebook if there is not a valid session it will open up the dialog and try to log in.
The LoginDialogListener() responds to the response of this.
public class LoginDialogListener extends BaseDialogListener {
#Override
public void onComplete(Bundle values) {
Log.d(TAG, "Login response recieved");
prefs.saveToken(facebook.getAccessToken());
prefs.saveExpiry(facebook.getAccessExpires());
facebook.extendAccessTokenIfNeeded(MyActivity.this, null);
Log.d(TAG, "Logged in ");
postToFacebook();
}
}
The other option is to look at using SSO (which I have commented out in the example code).