smsManager.sendTextMessage not working for message that exceeds one sms character limit
Asked Answered
A

1

5

I am working with a project which sends firebase dynamic link invitation to friends via sms. My code runs perfecly fine and sends sms when I send smaller links as invitation. like

    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, "Check It Out. This one is very nice and useful https://v5uht.app.goo.gl/Zi7X", null, null);
        Toast.makeText(getApplicationContext(), "Cheers :D :D", Toast.LENGTH_LONG).show();
    }

    catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

But it doesn't send sms when I include bigger link which exceeds one sms character limit though it shows the toast notification.

    String myNewLink = "https://v5uht.app.goo.gl/?link=http://expensecount.com/&apn=com.chtl.ribath.fdynamic1&amv=1&afl=https://play.google.com/store/apps/details?id%3Dcom.belief.colorgalaxy&myPage=2";
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(number, null, myNewLink, null, null);
        Toast.makeText(getApplicationContext(), "Cheers :D :D", Toast.LENGTH_LONG).show();
    }

    catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

what shall I do to include the whole link which is in myNewLink and make it working. Thank you.

Alp answered 22/10, 2016 at 8:42 Comment(2)
You must send it in parts.Tell me if you need any code :)Skyeskyhigh
i searched for some example but got nothing... if you plz share some code it would be hugely helpful :D @MohammadZAlp
S
14

This code might help you:

try {

    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> msgArray = smsManager.divideMessage(msg);

    smsManager.sendMultipartTextMessage(phoneNo, null,msgArray, null, null);
    Toast.makeText(getApplicationContext(), "Message Sent",Toast.LENGTH_LONG).show();
} catch (Exception ex) {
    Toast.makeText(getApplicationContext(), ex.getMessage().toString(), Toast.LENGTH_LONG).show();
    ex.printStackTrace();
}
Skyeskyhigh answered 22/10, 2016 at 9:23 Comment(4)
I was trying to send SMS using sendTextMessage of SmsManager class with all permissions enabled on Android Nougat (Nexus). SMS were not sent. Just copied this code. It works perfectly now. Hope this helps others :)Supralapsarian
@Supralapsarian Glad to hear that... :)Skyeskyhigh
great answer and great question! It was out of my mind the problem on too long message. Thanks guys!Townie
I'm glad to hear that :)Skyeskyhigh

© 2022 - 2024 — McMap. All rights reserved.