6

In my Android Aplication I just need to open SMS intent with pre populated message_body and the PhoneNumber.

Following is the code I am trying

Uri uri = Uri.parse(String.format("smsto:%s", strPhoneNumber));
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("sms_body", "Sample Body");
startActivityForResult(smsIntent, OPEN_SMS_APP);

All works great in default scenario but if Facebook Messenger is installed and setup it as the default SMS Application (settings -> Apps & Notifications -> Default Apps -> SMS app) then the functionality breaks.

Problem is, it opens FB messenger without the message_body (empty) even though it correctly picks the phone number (in FB Messenger APP).

Further, I tried following tests but didn't pick SMS_BODY or opened default Android APP

smsIntent.addCategory(Intent.CATEGORY_APP_MESSAGING); // STILL DIDN'T FIX
smsIntent.putExtra(Intent.EXTRA_TEXT, "Sample Body"); // STILL DIDN'T FIX

Questions

  1. Is there a way that I can force to open default Android SMS Application (Messages APP) even if someone have setup any other 3rd party SMS application as default App?
  2. OR Any other way I can pass message_body parameter to work in other 3rd party applications as well?
JibW
  • 4,538
  • 17
  • 66
  • 101
  • 1
    Facebook Messenger is broken. Report it as a bug to facebook, they should look at the body field. They don't. – Gabe Sechan Mar 16 '18 at 13:27
  • Try with `new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null))` only and do not add `Intent.CATEGORY_APP_MESSAGING`. I think URI `sms` should not pick fbmessanger app . Or am i missing something here? – ADM Mar 16 '18 at 13:29
  • 1
    Actually I just reported it via an ex-employee feedback channel. We'll see if it gets any attention paid to it. – Gabe Sechan Mar 16 '18 at 13:32
  • @ADM, tried that as well. Problem is it shows all the SMS apps but if user select FB Messenger then the same prob again. Also if user select the checkbox to Always open FB Messenger first time then even it won't show the list again. Thanks – JibW Mar 16 '18 at 13:32
  • @ADM you're missing something- messenger now does SMSes, and some people like having all their messages come in one place. You couldn't pay me enough to give FB access to all my private communication, but some people love it. – Gabe Sechan Mar 16 '18 at 13:33
  • @Gabe Sechan, Thank you for reporting it for FB. Lets see – JibW Mar 16 '18 at 13:40
  • 1
    According to the messenger team, the issue snuck in in early February, and will be fixed in the next version. – Gabe Sechan Mar 16 '18 at 17:55
  • Thank you Gabe Sechan. That would be great. – JibW Mar 20 '18 at 10:11
  • This issue has not been fixed yet in 2021... – iForests Aug 11 '21 at 18:34

2 Answers2

4

If you really want to restrict to Google Android SMS App then can specify the package name of the Google App. But there are so many other Applications that will read 'sms_body' Intent Key.

Uri uri = Uri.parse(String.format("smsto:%s", strPhoneNumber));
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("sms_body", "Sample Body");
// To Force Google Android SMS APP To Open
smsIntent.setPackage("com.google.android.apps.messaging"); 
if (smsIntent.resolveActivity(getPackageManager()) != null)
{
    startActivityForResult(smsIntent, OPEN_SMS_APP);
}
else
{
    // Display error message to say Google Android SMS APP required.
}

But I would use the Application Chooser rather than just restricting to Google SMS App. Obviously user have to go through an additional step (click), but it lists all SMS Apps (every time) and user have the option to select a correctly working SMS Application.

startActivityForResult(Intent.createChooser(smsIntent, "Sample Title"), OPEN_SMS_APP); 
Janaka H
  • 49
  • 3
0

it opens FB messenger without the message_body (empty)

There is no requirement for any app to honor undocumented Intent extras.

Further, I tried following tests but didn't pick SMS_BODY or opened default Android APP

EXTRA_TEXT is not documented for use with ACTION_SENDTO, nor is CATEGORY_APP_MESSAGING.

Is there a way that I can force to open default Android SMS Application (Messages APP)

There are ~10,000 Android device models. I would expect there to be dozens, if not hundreds, of pre-installed "Android SMS Application". None of them have to honor Intent extras or categories not specifically documented to be supported by ACTION_SENDTO.

Any other way I can pass message_body parameter to work in other 3rd party applications as well?

ACTION_SEND — not ACTION_SENDTO — offers EXTRA_TEXT. However, you cannot mandate where the content should be sent. That would be up to the user.

You can also use SmsManager to send the SMS directly, if you hold the SEND_SMS permission.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi CommonsWare,Thanks for the explanation. In this link (https://developer.android.com/guide/components/intents-common.html#Messaging) Android explains the Intent Extras that should pass for Text Messaging. There the "sms_body" is the SMS Message content. Problem is Android allows to set 3rd party APPS to be default SMS APP and the functionality breaks as they don't adhere Android standards. – JibW Mar 20 '18 at 09:56
  • If that is the case wonder if there is a way that I can force to open Android SMS APP rather than any other even if set one of them as default. I have checked SmsManager but really want to use intent to support similar functionality as IOS APP. – JibW Mar 20 '18 at 09:58
  • @JibW: "I can force to open Android SMS APP" -- there is no single "Android SMS App". – CommonsWare Mar 20 '18 at 11:28
  • Thank you @CommonsWare. Ya, I mean force to open what ever default GOOGLE ANROID SMS APP if possible (NOT any preferred 3rd party SMS APP if have set as default SMS APP). If NOT found any then stop user saying you need to have a GOOGLE ANDROID SMS APP (NOT any 3rd party APPS). If that's NOT possible then a working solution for all scenarios as GOOGLE have well documented the intent extras for Text Messaging. Thanks – JibW Mar 20 '18 at 11:45
  • @JibW: "default GOOGLE ANROID SMS APP" -- many devices do not ship with an SMS app from Google. – CommonsWare Mar 20 '18 at 11:48
  • In those cases to stop user porceeding further saying the error message that it's required to have a GOOGLE ANDROID SMS APP (NOT any 3rd party apps). Like in most of the cases default Google Android installed "Messages App" or what ever other Google Android SMS app (who follows the correct behaviour). – JibW Mar 20 '18 at 11:57
  • @JibW: "Like in most of the cases" -- replace "most" with "some" or possibly "few". For example, the Samsung Galaxy S8 does not ship with "a GOOGLE ANDROID SMS APP". You are certainly welcome to provide evidence that most of the ~2 billion Android devices have "a GOOGLE ANDROID SMS APP". – CommonsWare Mar 20 '18 at 12:17
  • Please check this link of Samsung S8 (https://www.youtube.com/watch?v=P0NM-MzS76M). Either it have or can install GOOGLE ANDROID SMS APP ("Messages" App in this case). My point is because FB Messeger is not reading "sms_body" intent extra key (very well documented in Google Android docs) my APP is fully broken loading it with EMPTY MESSAGE BODY if user have set FB Messenger as default SMS App. Working ok with Android SMS App. Thanks – JibW Mar 20 '18 at 12:26
  • @JibW: "Either it have or can install GOOGLE ANDROID SMS APP ("Messages" App in this case)" -- the Messages app on my Samsung Galaxy S8 is a Samsung app, not a Google app. – CommonsWare Mar 20 '18 at 12:42
  • Samsung can definitely install Google Android Messages App (that youtube link clearly shows that). Also google have documented "sms_body" intent key to pass Mesage Body. Are you saying even that Samsung App doesn't read that as well? Are you? Finally, what is your suggestion to solve this issue (to work in all scenarios) in my question? – JibW Mar 20 '18 at 13:54
  • @JibW: "Are you saying even that Samsung App doesn't read that as well?" -- I have no idea. My point is that you are making unsafe assumptions, then are blaming users ("saying you need to have a GOOGLE ANDROID SMS APP (NOT any 3rd party APPS)") because of those assumptions. "Finally, what is your suggestion to solve this issue" -- avoid the issue entirely. Do not rely upon `sms_body`. Make no assumptions about what the other app does with your `ACTION_SENDTO` request. Or, send the SMS yourself. Or, eliminate this feature from your app. – CommonsWare Mar 20 '18 at 15:04
  • Unfortunately I can NOT eliminate SMS feature from this App as that's part of the main business requirement. In brief the user (Merchant) of this APP creates an order and sending the Payment LINK through SMS message to their customers... Basically I am doing exactly what's explained in Google Android API documentation and sadly it is broken right now if FB Messenger set as default SMS APP. – JibW Mar 20 '18 at 15:42
  • @JibW Where you able to fix the issue? – cortex Sep 03 '19 at 20:58
  • @cortex, No it isn't. – JibW Sep 30 '19 at 09:21