2

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

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Thushara
  • 559
  • 7
  • 22

2 Answers2

0
  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.
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • 1
    Thank you for the help. I did as you suggests. commented public void onPause() { unregisterReceiver(smsBroadcast); and removed manifest boradcast part. This is fine. " startSmsRetriever: isSuccessful" But still broadcast onReceive not called after receiving the sms. Actually I generate the required sms from the another phone and send it to the testing huawei phone. I mean for now sms not coming from the web server. is it be an issue here? – Thushara Apr 22 '21 at 07:48
  • hi@Thushara 1. The SMS message does not come from the web server is ok too. 2. Have you received a timeout broadcast after more than 5 minutes? 2. Does the format of the message you send meet the requirements? Could you pls post the full text message you sent? – zhangxaochen Apr 22 '21 at 08:03
  • and btw,Ensure that the `hash_value` field is correct. – zhangxaochen Apr 22 '21 at 08:04
0

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

Thushara
  • 559
  • 7
  • 22