我已经SMS Retriever API
在谷歌教程和我的调试 Build Variant 中实现了类似的工作。我可以阅读短信并将代码发送给用户可以登录。
我的问题是当我在 release Build Variant 中运行应用程序时,它不起作用。我收到了短信,但我无法读取登录代码。
我在发布模式下更改了AppSignatureHelper生成的哈希,这与调试模式不同。在调试工作和版本号中。
一些帮助将不胜感激
编码:
显现:
<receiver android:name=".app.receivers.SmsReceiver">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
在我的课程中:(在发布和调试模式下,代码会抛出 onSucess 方法)此方法在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);
}
});
}
我的接收器:
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;
}
}
我的 smsReceiver 方法:
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);
}
}
});
}