6

When I'm trying to share text using intent mechanism and pick WhatsApp, it says:

Can't send empty message

I've read an official docs about Android integration here: https://faq.whatsapp.com/en/android/28000012

My code:

public void shareText(String label, CharSequence title, CharSequence body) {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, title.toString());
        intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body));

        final Intent chooser = Intent.createChooser(intent, label);
        chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (chooser.resolveActivity(mContext.getPackageManager()) != null) {
            mContext.startActivity(chooser);
        }
 }

Am I doing something wrong? Or is it bug with WhatsApp messenger?

P.S. arguments title and body are not empty in my case.

Oleksandr Albul
  • 1,611
  • 1
  • 23
  • 31

3 Answers3

9

What you have done is,

intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body));

while TextUtils.concat(title, body) returns CharSequence probably that whatsapp does not support.

You have to pass the value as a String leaving you two solutions.

  • You can convert the whole to a String by toString()

intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body).toString());

  • Converting it a String before passing it to intent.

String someValue = TextUtils.concat(title, body).toString();

and adding it here as,

intent.putExtra(Intent.EXTRA_TEXT, someValue);
sanjeev
  • 1,664
  • 19
  • 35
  • 1
    you are correct. But it is frustrating experience, as other messengers and apps could accept CharSequence and strip formatting if it is not supported. So I will set plain String to Intent.EXTRA_TEXT and also add html presentation of Spannable like this: `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { intent.putExtra(Intent.EXTRA_HTML_TEXT, Html.toHtml(TextUtils.concat(title, body))); }` – Oleksandr Albul Aug 13 '19 at 09:41
  • Yeah that seems like a pretty good way to approach and makes more sense. – sanjeev Aug 13 '19 at 09:52
1

Here You can send data from your app to Whatsapp and any other like a messenger

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT,   " Your text ");
startActivity(Intent.createChooser(share,  " Your text "));
BlackBlind
  • 772
  • 9
  • 26
  • This is like copy-paste from official docs. Have tried this exact example and it is working. But what is wrong with my case? I do not need to send " Your text " litteral – Oleksandr Albul Aug 13 '19 at 09:11
0

Here sendEmtpyMassages is Button Just copy this method It's Work for you

sendEmtpyMassages.setOnClickListener {
        val context: Context = applicationContext
        val sendIntent = Intent("android.intent.action.MAIN")
        sendIntent.action = Intent.ACTION_VIEW
        sendIntent.setPackage("com.whatsapp")
        val url = "https://api.whatsapp.com/send?phone=" + "&text=" + " "
        sendIntent.data = Uri.parse(url)
        if (sendIntent.resolveActivity(context.packageManager) != null) {
            startActivity(sendIntent)
        }
    }