Reputation: 4086
I am developing an application in which user can share messages with his/her Facebook friends. I am using Facebook API for Android.
I can able to authenticate user, as well as I can get my friend list as a Facebook user and also post message on wall, but I am looking for sending private message to my friends and I didn't get any solution for that, so can any body help me, how can I achieve this?
Upvotes: 15
Views: 17206
Reputation: 191
It is possible to send facebook private message using below code.
if (isPackageExisted("com.facebook.orca")) {
Uri uri = Uri.parse("fb-messenger://user/");
uri = ContentUris.withAppendedId(uri, Long.parseLong("Enter user id here"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else {
Toast.makeText(this, "Please install facebook messenger", Toast.LENGTH_LONG).show();
}
}
Check Facebook messenger is install or not
public boolean isPackageExisted(String targetPackage) {
PackageManager pm = getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(targetPackage, PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
Upvotes: 0
Reputation: 1568
You can use MessengerUtils to send the message with attachments.
You can send attachment with following mime types:
Sample code to send image is like below
String mimeType = "image/jpeg";
// contentUri points to the content being shared to Messenger
ShareToMessengerParams shareToMessengerParams =
ShareToMessengerParams.newBuilder(contentUri, mimeType)
.build();
// Sharing from an Activity
MessengerUtils.shareToMessenger(
this,
REQUEST_CODE_SHARE_TO_MESSENGER,
shareToMessengerParams);
More documentation is on https://developers.facebook.com/docs/messenger/android
Upvotes: 0
Reputation: 2302
Latest Android SDK features now the (private) Message Dialog
https://developers.facebook.com/docs/android/message-dialog/
Upvotes: 0
Reputation: 164147
It is not possible to send private messages on the behalf of the user using the graph api.
You should however be able to use the Send Dialog, though I haven't tried it on android, but it should be something like:
Bundle params = new Bundle();
params.putString("to", "USER_ID");
params.putString("name", "TITLE HERE");
params.putString("link", "A URL"); // this link param is required
facebook.dialog(context, "send", params, new DialogListener() {
@Override
public void onComplete(Bundle values) {
....
}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
Another approach you can use is the Chat API with which you can send messages on the behalf of the user, it requires the xmpp_login
permission and you to implement an xmpp client.
Since this dialog is not supported yet in android, you have 3 options:
xmpp_login
and add a xmpp client (i.e.: asmack) and with that you can implement your own "Send Message" dialog.Upvotes: 7