Search code examples
androidfacebook-messengerandroid-sharing

How to share text and link from android app to messenger?


I want to have possibility to share some text and link from my app to Messenger and get message like this: (Message shared from Elephant Evolution app)

Message shared from Elephant Evolution app

This is my code:

                //SHARING TO FACEBOOK
            String photoURL = "https://play.google.com/";
            if(!mEvent.getPhotoUrl().isEmpty()){
                photoURL=mEvent.getPhotoUrl();
            }
            String quoteToShare = "someText";
            ShareLinkContent content = new ShareLinkContent.Builder()
                    .setContentUrl(Uri.parse(photoURL))
                    .setQuote(quoteToShare)
                    .build();
            //ShareDialog.show(EventActivity.this,content);
            MessageDialog.show(EventActivity.this, content);

And with this code I am sharing only link:

enter image description here

When I am sharing the same "ShareLinkContent" to facebook, everything works good. Could anyone help me :) ?


Solution

  • You can share text (and link) using Intent class:

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button = findViewById(R.id.button);
            String textToShare = "My own text\nhttps://stackoverflow.com/";
    
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.facebook.orca");
    
            button.setOnClickListener(v -> {
                try {
                    startActivity(sendIntent);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            });
        }
    }
    

    Package for Facebook Messenger is: com.facebook.orca