Huawei auto read sms

953 views Asked by At

I want to implement auto read sms in Huawei. I referenced this https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/readsmsmanager-0000001050050861-V5 and setup everything as required. But broadcast not working. Here is the code.

Manifest service declaration

<receiver
    android:name=".util.SMSBroadCastReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="com.huawei.hms.support.sms.common.ReadSmsConstant.READ_SMS_BROADCAST_ACTION" />
    </intent-filter>
</receiver>

Broadcast class

public class SMSBroadCastReceiver extends BroadcastReceiver {

    private static final String TAG = "SMSBroadCastReceiver";
    private OTPReceiveListener otpReceiver = null;

    public void initOTPListener(OTPReceiveListener receiver) {
        this.otpReceiver = receiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null && ReadSmsConstant.READ_SMS_BROADCAST_ACTION.equals(intent.getAction())) {
            Status status = bundle.getParcelable(ReadSmsConstant.EXTRA_STATUS);
            if (status.getStatusCode() == CommonStatusCodes.TIMEOUT) {
                // The service has timed out and no SMS message that meets the requirements is read. The service process ends.
                Log.i(TAG, "onReceive: TIMEOUT ");
                this.otpReceiver.onOTPTimeOut();
            } else if (status.getStatusCode() == CommonStatusCodes.SUCCESS) {
                if (bundle.containsKey(ReadSmsConstant.EXTRA_SMS_MESSAGE)) {
                    // An SMS message that meets the requirement is read. The service process ends.
                    Log.i(TAG, "onReceive: received " + bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE));
                    this.otpReceiver.onOTPReceived(bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE));
                }
            }
        }
    }

    public void startSmsRetriever(Context context) {
        Task<Void> task = ReadSmsManager.start(context);
        task.addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    // The service is enabled successfully. Perform other operations as needed.
//                    doSomethingWhenTaskSuccess();
                    Log.i(TAG, "startSmsRetriever: isSuccessful");
                }else{
                    //task false
                    Log.i(TAG, "startSmsRetriever: failed");
                }
            }
        });
    }

    public interface OTPReceiveListener {

        void onOTPReceived(String otp);

        void onOTPTimeOut();
    }
}

Activity class codes

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        smsBroadcast = new SMSBroadCastReceiver();
        smsBroadcast.initOTPListener(this);
        smsBroadcast.startSmsRetriever(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ReadSmsConstant.READ_SMS_BROADCAST_ACTION);
        registerReceiver(smsBroadcast, intentFilter);
    }

    @Override
    public void onPause() {
        unregisterReceiver(smsBroadcast);
        super.onPause();
    }
    

I also generated required hashcode and send the sms.

Here startSmsRetriever: isSuccessful. But broadcast onReceive not called. Please help me on this

2

There are 2 answers

3
zhangxaochen On
  1. Delete the onPause method first. Beacuse after the automatic SMS message obtaining service is enabled, the timeout period is 5 minutes. If the onReceive method is triggered after 5 minutes, the broadcast function is enabled successfully.

    Delete this method

    enter image description here

    Five minutes later, check whether the onReceive method is triggered. If yes, the broadcast is normal.

    enter image description here

  2. The SMS message format must comply with the following example. If the SMS message format is incorrect, the broadcast is not triggered even if the mobile phone receives the SMS message. The broadcast times out after 5 minutes.

prefix_flag short message verification code is XXXXXX hash_value

enter image description here

  1. Broadcast registration cannot be registered in the manifest ,only can be dynamically registered in the activity.
0
Thushara On

I updated HMS core in apps and restart the device and magically it works.