Android: Open SMS Intent
Asked Answered
H

2

7

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?
Holzer answered 16/3, 2018 at 13:15 Comment(9)
Facebook Messenger is broken. Report it as a bug to facebook, they should look at the body field. They don't.Callender
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?Samaveda
Actually I just reported it via an ex-employee feedback channel. We'll see if it gets any attention paid to it.Callender
@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. ThanksHolzer
@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.Callender
@Gabe Sechan, Thank you for reporting it for FB. Lets seeHolzer
According to the messenger team, the issue snuck in in early February, and will be fixed in the next version.Callender
Thank you Gabe Sechan. That would be great.Holzer
This issue has not been fixed yet in 2021...Anderegg
W
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); 
Woodenware answered 23/3, 2018 at 16:28 Comment(0)
S
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.

Symploce answered 16/3, 2018 at 14:9 Comment(14)
Hi CommonsWare,Thanks for the explanation. In this link (developer.android.com/guide/components/…) 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.Holzer
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.Holzer
@JibW: "I can force to open Android SMS APP" -- there is no single "Android SMS App".Symploce
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. ThanksHolzer
@JibW: "default GOOGLE ANROID SMS APP" -- many devices do not ship with an SMS app from Google.Symploce
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).Holzer
@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".Symploce
Please check this link of Samsung S8 (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. ThanksHolzer
@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.Symploce
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?Holzer
@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.Symploce
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.Holzer
@Holzer Where you able to fix the issue?Revile
@cortex, No it isn't.Holzer

© 2022 - 2024 — McMap. All rights reserved.