I've implemented the SMS Retriever API
like in the google tutorials and in my debug Build Variant work fine. I can read the sms and get the code to the user can do the login.
My problem is when I run the app in release Build Variant the sms it doesn't work. I receive the sms but I can't read the code to do the login.
I change the hash generated with AppSignatureHelper in release mode that is differente than in the debug mode. In debug work and in release no.
Some help will be appreciate
The code:
Manifest:
<receiver android:name=".app.receivers.SmsReceiver">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
In my class: (In release and in debug mode the code go throw the onSucess method) This method is called in onCreate.
private void startSMSListening(){
SmsRetrieverClient client = SmsRetriever.getClient(this);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Successfully started retriever, expect broadcast intent
Log.e("startSMSListening", "listening sms");
sendCode();
showHideLoadingView(false);
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Failed to start retriever, inspect Exception for more details
Log.e("startSMSListening", "failure listening sms");
showHideLoadingView(false);
}
});
}
My receiver:
public class SmsReceiver extends BroadcastReceiver {
//interface
private static SmsListener mListener;
@Override
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
if(extras != null) {
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
if(status != null) {
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
//Pass the message text to interface
if (mListener != null && !StringUtil.isNull(message)) {
mListener.messageReceived(message);
}
break;
case CommonStatusCodes.TIMEOUT:
Log.d("SMSReceiver", "timed out (5 minutes)");
break;
}
}
}
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
}
My smsReceiver method:
private void smsReceiver(){
SmsReceiver.bindListener(new SmsListener() {
@Override
public void messageReceived(String messageText) {
//From the received text string you may do string operations to get the required OTP
//It depends on your SMS format
Log.e("Message",messageText);
// If your OTP is six digits number, you may use the below code
Pattern pattern = Pattern.compile(OTP_REGEX);
Matcher matcher = pattern.matcher(messageText);
String otp = null;
while (matcher.find()) {
otp = matcher.group();
}
if(otp != null && et_code != null) {
et_code.setText(otp);
}
}
});
}
When the user device receives a message, Google play services checks the app hash. It then sends the message text to your app over the SMS Retriever API. The app then reads and extracts the code in the SMS message. This code is usually sent back to the server for verification.
I've implemented the SMS Retriever API like in the google tutorials and in my debug Build Variant work fine. I can read the sms and get the code to the user can do the login. My problem is when I run the app in release Build Variant the sms it doesn't work. I receive the sms but I can't read the code to do the login.
For example, if you are not the sender or you don’t want to add anything to the SMS you should use SMS User Consent API. But if you are a sender and you accepting the adding anything to the SMS, then you should use SMS Retriever API. SMS Retriever API positive aspect is you don’t need any permission for reading the SMS.
Imagine an application where your use case is to get the SMS only for the use of validating the user with OTP. In your whole application you are not using that SMS reading feature again. It is waste o f the resources and time and code to check the SMS permission. Here Google came to save you with the SMS retriever API.
With the SMS Retriever API, we can perform SMS-based user verification in our Android app automatically, without requiring the user to manually type verification codes, and without requiring any extra app permissions In this article, we will learn our SMS Retriever API and see how this can be easily used for SMS verification. So, let’s get started.
First download your app signing certificate .der file then convert to .jks file by this command
keytool -import -alias your_alias -keystore file_name_created -file certificate.der
then new .jks file created
then use this command for generate hash for your release
keytool -exportcert -alias your_alias -keystore certificate.jks | xxd -p | tr -d "[:space:]" | echo -n app_package_name `cat` | sha256sum | tr -d "[:space:]-" | xxd -r -p | base64 | cut -c1-11
then create hash string and it will work on play store app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With