0

I am working on an Android app which uses a broadcastReceiver to extract and show certain text messages. These messages will always be quite long and the broadcastReceiver is failing to get them. After gradually cutting the test messages smaller and smaller I have found the upper limit is about 154 characters (give or take a few).

My code is

public class SmsReceiver extends BroadcastReceiver {

    public static final String EXTRA_MESSAGE = "com.example.alert10.MESSAGE";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
            String smsSender = "";
            String smsBody = "";
            for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                smsSender = smsMessage.getOriginatingAddress();
                smsBody = smsMessage.getMessageBody();
            }

            if (smsSender.equals("+447800966043")) {
                if (smsBody.contains("Attendance Request")) { 
                    Intent secondintent = new Intent(context, MainActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(EXTRA_MESSAGE, smsBody);
                    bundle.putInt("VIS", 1);
                    secondintent.putExtras(bundle);
                    secondintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(secondintent);
                    abortBroadcast();
                }
            }
        }
    }
}

Why is this happening? What can I do to make it work with long messages?

TrapezeArtist
  • 777
  • 1
  • 13
  • 38
  • I'm no expert, but how about this? https://developer.android.com/reference/android/telephony/SmsMessage#MAX_USER_DATA_BYTES, a 140 byte limit. – Gene Feb 08 '21 at 16:30
  • Looks like you'll need to deal with multi-part messages. Here's some info. No idea of its quality: https://stackoverflow.com/questions/14326556/how-to-get-allowed-maximum-length-for-an-sms – Gene Feb 08 '21 at 16:34
  • That starts to make sense, Gene. 140 bytes is 140 or 160 characters. My estimate of 154 characters was only an estimate, so it fits. I can't make much sense of the info in the stackoverflow link but at least it points me in the right direction and I have a couple of new keywords to google. – TrapezeArtist Feb 08 '21 at 17:07

1 Answers1

0

Thanks to Gene I was alerted to the fact (that I'd forgotten) that long SMS messages are actually multiple messages concatenated together, and that's what was breaking my app. So after a bit of googling, and in particular finding om252345's answer in Android - receiving long SMS (multipart), I came up with this modified code:

public class SmsReceiver extends BroadcastReceiver {

    public static final String EXTRA_MESSAGE = "com.example.alert10.MESSAGE";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
            String smsSender = "";
            String smsBody = "";
            for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                smsSender = smsMessage.getOriginatingAddress();

                Bundle bundle = intent.getExtras();
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] =
                            SmsMessage.createFromPdu((byte[]) pdus[i]);
                }

                SmsMessage sms = messages[0];
                try {
                    if (messages.length == 1 || sms.isReplace()) {
                        smsBody = sms.getDisplayMessageBody();
                    } else {
                        StringBuilder bodyText = new StringBuilder();
                        for (int i = 0; i < messages.length; i++) {
                            bodyText.append(messages[i].getMessageBody());
                        }
                        smsBody = bodyText.toString();
                    }
                } catch (Exception e) {
                    Toast.makeText(context, "App has failed to collect Alert",
                            Toast.LENGTH_LONG).show();
                }
            }

            if (smsSender.equals("+447800966043")) {
                if (smsBody.contains("Attendance Request")) {
                    Intent secondintent = new Intent(context, MainActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(EXTRA_MESSAGE, smsBody);
                    bundle.putInt("VIS", 1);
                    secondintent.putExtras(bundle);
                    secondintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(secondintent);
                    abortBroadcast();
                }
            }
        }
    }
}

Hopefully this might help anyone else struggling with the same issue in the future.

TrapezeArtist
  • 777
  • 1
  • 13
  • 38