Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android, sms.send: Suggestion to use sendMultipartTextMessage instead of sendTextMessage #831

Open
Konubinix opened this issue Feb 4, 2025 · 1 comment

Comments

@Konubinix
Copy link

I was trying to send sms from plyer recently and struggled to understand why some of them did not work. I learned in the process that:

  1. sms are actually limited to 160 encoded characters,
  2. when they don't contains ascii characters, the message is utf-16 encoded hence the limit on the message becomes quite low.
  3. there are two methods provided by android.telephony.SmsManager
    1. divideMessage gets the message and returns an array of single sms messages
    2. sendMultipartTextMessage gets as input the array returned by divideMessage and sends several sms as if it was a single message

I realize that this is was happens behind the hood in the default sms app.

Image

You can see here that the application informs me that the message will be actually split in two and that I only have 20 characters left in my message.

When removing the ê, I can type more words before the application let me know of the limit.

Image

So, I tried replacing sms.sendTextMessage(recipient, None, message, None, None) by sms.sendMultipartTextMessage(recipient, None, sms.divideMessage(message), None, None)

And it worked like a charm. I don't have message silently failing anymore.

I'm asking therefore if a pull request doing just that would be accepted? I don't know anough about android and the sms world to tell if there are reasons not to do that.

What do you think?

@Konubinix
Copy link
Author

In any case, if we want to stick with sendTextMessage, it would be good to add a check to raise an error when the sms will not be sent.

Before finding sendMultipartTextMessage, I added this code, so that at least, I knew when this situation happened:

for encoding in "ascii", "utf-16":
    try:
        length = len(message.encode(encoding))
        if length >= 160:
            raise Exception(f"Message to long {length}")
        else:
            break
    except UnicodeEncodeError:
        pass
else:
    raise Exception("I should not reach that part")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant