Search code examples
androidsmsbroadcastreceiver

Android SMS broadcast receiver , Reading SMS


My App reads OTP SMS that I send via a 3rd party SMS gateway. It has been working well, we tested on many devices. Then came a MOTO E with a dual sim, which does not get authenticated even it received the OTP SMS.

Are there any modifications required for reading OTP messages for dual sim mobiles with both sim active?

Following is my SMS receiver class:

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();
    private static String message;
    Context myContext;
   // public static final int recDigits= Integer.parseInt(message.substring(20,26));

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        context.unregisterReceiver(this);

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                    if(LoginAuth.otpVerify == Integer.parseInt(message.substring(20,26).trim())){
                        Toast.makeText(context,"Sign Up Successful",Toast.LENGTH_LONG).show();

                        sendMessage();
                    }





                    /*intent.putExtra("msg", message.substring(20,26).trim());

                    // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context,
                            message.substring(20,26), duration);
                    toast.show();*/

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);

        }


    }


    private void sendMessage() {
        Log.d("sender", "Broadcasting message");
        Intent intent = new Intent("custom-event-name");

        intent.putExtra("message", "This is my message!");
        LocalBroadcastManager.getInstance(myContext).sendBroadcast(intent);
    }

}

The above class reads the OTP message, substrings the OTP and verifies it. If verified, the user is intended to the next screen. If the user doesn't receives the OTP within 90 sec, he is asked to submit again.

Now, when I tested on MOTO E dual sim, OTP was generated and received on the mobile within 5-10 seconds , but My SMS receiver does not verify it.

Is it the problem with the dual sim or the receiver?


Solution

  • I solved the above issue by providing the Priority for the filter as given in google docs.

      private void registerSmsReciever() {
            smsReciever = new IncomingSms();
            IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
            intentFilter.setPriority(2147483647);
            this.registerReceiver(smsReciever, intentFilter);
        }