15

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ramakrishna
  • 4,066
  • 16
  • 48
  • 72

4 Answers4

7

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.


Edit

Since this dialog is not supported yet in android, you have 3 options:

  1. Wait for facebook to implement the dialog for android.
  2. Try to open the dialog in a browser (the url for that is in the docs) in the mobile device.
  3. Ask for the xmpp_login and add a xmpp client (i.e.: asmack) and with that you can implement your own "Send Message" dialog.
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Thanks for your answer. already i used above code but in Send Dialog it says "This Dialog is currently not supported on mobile devices". – Ramakrishna May 26 '12 at 11:21
  • Eh, I was afraid of that (since it's not in the docs), but was worth the try. I edited my answer with more info. – Nitzan Tomer May 26 '12 at 11:31
  • hi, u said //Try to open the dialog in a browser (the url for that is in the docs) in the mobile device.// which url u r indicating, can u give me the doc link or url link please – Kishor datta gupta Sep 02 '14 at 14:30
  • It's right there in the page I linked (`Send Dialog`): `http://www.facebook.com/dialog/send? app_id=123050457758183 &link=http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html &redirect_uri=https://www.bancsabadell.com/cs/Satellite/SabAtl/` – Nitzan Tomer Sep 02 '14 at 14:33
  • There is a message that Chat API is depricated on provided Chat API link. Does it means that they don't support Chat API anymore? Or this API moved to Facebook 2.0 API? I couldn't find any description of current version of Chat API. – Sergey Dirin Dec 27 '14 at 11:55
0

Latest Android SDK features now the (private) Message Dialog
https://developers.facebook.com/docs/android/message-dialog/

younes0
  • 2,288
  • 1
  • 25
  • 34
0

You can use MessengerUtils to send the message with attachments.

enter image description here

You can send attachment with following mime types:

enter image description here

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

enter image description here

More documentation is on https://developers.facebook.com/docs/messenger/android

Piyush
  • 1,528
  • 2
  • 24
  • 39
0

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;
}
Foram Shah
  • 191
  • 6