How to share message on facebook wall from my Android app? - android

I think, this is very simple problem. I have one button in my app. If i click it it executes one method. In this method i want to share "Hi this is xxx using xxxxx product" message on user's facebook wall. How can i do that? Is there any tutorials?
myMethod(){
//share on fb wall
}

From Sharing in Android tutorial:
Prerequisites
Before you can share to Facebook from your app, or attempt to follow
any of the sharing tutorials, you will need:
Your environment set up A Facebook app properly configured and linked
to your Android app, with SSO enabled The Facebook SDK added to your
project If you haven't done this and need help doing so, you can
follow our getting started guide.
Then essentially, have your myMethod call this:
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();
}

Related

Facebook Share Dialog is not opening

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.

facebook request dialog android shows only a partial list of friends

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

Sharing on facebook without the native app installed

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;
}

Facebook Feed Dialog showing error

I have integrated facebook sdk with my native android app.
Currently i am trying to open facebook feed dialog from base adapter class and it is showing me error.
I have taken reference from the below link.
https://developers.facebook.com/docs/android/feed-dialog
https://developers.facebook.com/docs/reference/dialogs/feed/
Everytime it shows me different errors .
I don't know what's wrong with my code.
I am also putting my code here.
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(CONTEXT,
Session.getActiveSession(),
params))
.setOnCompleteListener(new WebDialog.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(CONTEXT,
"Posted story, id: " + postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(CONTEXT,
"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(CONTEXT,
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
Try this, Go to the Facebook App. Edit its settings. On the Advanced settings page, disable the "Stream post URL security" option.
How to go.
Go to developers.facebook.com/apps On the left hand side, click your app to select it.
Next to the "Settings" section in the middle column, there's an "Edit Settings" link.
Click that. On the new page, under the "Settings" menu on the left hand side, click "Advanced".
Under the "Migrations" section, find "Stream post URL security". Set it to "Disabled". Click the "Save Changes" button at the bottom of the screen..
It will look something like this
You can see by default this is on just click it OFF and click on Save Changes and then try to clean your project and run it again. Might be this will help you.

How to share a message to facebook -- android

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.

Categories

Resources