-1

I have integrated Auto read OTP in my code and it's working fine except in MIUI devices. BroadcastReceiver never wake up in case of MI devices when sms is received. After some efforts I think MI devices runs on its Permission Manager where all Permissions regarding read sms are disabled by default as comes in category of Privacy permissions and it looks like this : enter image description here

Now I can't figure out any way to Notify user first to accept or decline the Permission. Device simply disable the permissions by default after app is Installed in device.Is there any way to notify user to Accept the Permission's when app Installed ?

Some code:

SMSReciever.class - BroadcastReceiver wakes up when SMS is received.

public class SMSReciever extends BroadcastReceiver {
private static final String OTP_LENGTH = "6";
private String otp = "";
private IMessageCallback callable;
private static final String TAG = SMSReciever.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;

    if (bundle != null) {

        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

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

            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

            try {
                String sender = msgs[i].getOriginatingAddress();

                String phoneNumber = msgs[i].getDisplayOriginatingAddress();
                Log.d(TAG, "onReceive: phone " + phoneNumber);
                String message = msgs[i].getMessageBody();
                Log.d(TAG, "onReceive: phone " + message);

                otp = GetOtp.extractOTP(message, OTP_LENGTH);

                if (null != sender && message.contains("OTP for AppName") && !TextUtils.isEmpty(otp)) {
                    if (callable != null)
                        callable.getOtp(otp);
                }

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    }
}

public void setMessageCallable(IMessageCallback callable) {
    this.callable = callable;
}

public interface IMessageCallback {
    void getOtp(String otp);
}
}

AndridManifest.xml - permissions to receive and read sms

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65

1 Answers1

0

MI devices can set permissions as per the App. Can you plz check if the your app has SMS permission enabled.

You need to go to Settings > Apps > {your_app} > permissions and check if the SMS permission is enabled. As Xiaomi phones provide per app permissions.

Puneet Ahuja
  • 147
  • 6
  • I did attach the screenshot of same in my question, permission are disabled by default but why ? and if disabled how to notify user from app to enable the permission – Kapil Rajput Mar 28 '17 at 10:49
  • Have you tried with giving a priority in Manifest file? – Puneet Ahuja Mar 28 '17 at 11:02
  • You can try with this but i am not sure weather it will work or not. I faced this issue with All the MI devices and didn't find the perfect Solution. – Puneet Ahuja Mar 28 '17 at 11:09