Ramakrishna
Ramakrishna

Reputation: 4086

Send private message to my friend(s) using my Android application

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

Answers (4)

Foram Shah
Foram Shah

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

Piyush
Piyush

Reputation: 1568

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

Upvotes: 0

younes0
younes0

Reputation: 2302

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

Upvotes: 0

Nitzan Tomer
Nitzan Tomer

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.


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.

Upvotes: 7

Related Questions