Reputation: 231
Here is my MySMSBroadcastReceiver
package com.example.gofresh;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.auth.api.phone.SmsRetriever;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.Status;
public class MySMSBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
Toast.makeText(context,"OTP: "+message,Toast.LENGTH_LONG).show();
Log.i("Tag","OTP: " +message);
break;
case CommonStatusCodes.TIMEOUT:
Toast.makeText(context,"TIMEOUT",Toast.LENGTH_LONG).show();
Log.i("Tag","FAIL");
break;
}
}
}
Here is my AndroidManifest
<receiver
android:name=".MySMSBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
In my first layout, there is a Name and Phone number Text Field, to be entered manually by the user. Below this is a button, titled "Let's go!". The second layout contains a pinView for the OTP. Below is my onClick Method which is called when the button is being clicked
public void onClick(View v) {
if (next.getText().equals("Let's go!")) {
progressBar.setVisibility(View.VISIBLE);
username = userName.getText().toString();
phoneNumber = "+91" + userPhone.getText().toString();
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(phoneNumber)) {
if (phoneNumber.length() == 13) {
next.setText("Verify");
first.setVisibility(View.GONE);
second.setVisibility(View.VISIBLE);
topText.setText("I still don't trust you.\nTell me something that only the two of us know.");
SmsRetrieverClient client = SmsRetriever.getClient(this );
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.i("TAG","Started Operation Successfully");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("TAG","Operation Failed");
}
});
} else {
Toast.makeText(SignIn.this, "Invalid Number Entered.\nNumber must be of 10 digits", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(SignIn.this, "Please enter the details", Toast.LENGTH_SHORT).show();
}
} else if (next.getText().equals("Verify")) {
String OTP_Entered = pinView.getText().toString();
if(OTP_Entered.equals(OTP_Received)){
pinView.setLineColor(Color.GREEN);
textU.setText("OTP Verified");
textU.setTextColor(Color.GREEN);
goToHomeScreen();
}else{
pinView.setLineColor(Color.RED);
textU.setText("Incorrect OTP");
textU.setTextColor(Color.RED);
}
}
}
Output Log:
2019-06-10 16:10:46.830 12864-12864/com.example.gofresh I/TAG: Started Operation Successfully
2019-06-10 16:18:14.372 12864-12864/com.example.gofresh I/Tag: FAIL
2019-06-10 16:18:14.395 12864-12864/com.example.gofresh I/Tag: FAIL
My Message Format:
<#> Your Example Code is: 123456 FA+9qCX9VSu
Upvotes: 6
Views: 4638
Reputation: 231
The answer to this is really simple. Each application has it's own unique Hash. How this hash is generated can be found over here Android - SMS Retriever API - Computing app's hash string problem
Hope this helps
Upvotes: 1