为什么SMS Retriever API在发布模式下不起作用? [英] Why SMS Retriever API don't work in release mode?
问题描述
我已经像Google教程中一样实现了SMS Retriever API
,并且在我的Debug Build Variant中可以正常工作.我可以阅读短信,并将代码获取给用户可以进行登录.
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.
我的问题是,当我在Build Variant版本中运行该应用程序时,该短信不起作用.我收到短信了,但是我无法阅读代码来登录.
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.
一些帮助将不胜感激
代码:
清单:
<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 中调用此方法.
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);
}
});
}
我的接收者:
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);
}
}
});
}
推荐答案
请按照以下步骤获取生产密钥:
Follow these steps to get the key for production:
- 转到构建选项.
- 在选项中,选择选择构建变体.
- 然后在左上角打开一个对话框,从此处将Build Variant从 debug 更改为 release .
- 单击运行,然后将打开以下对话框:
- 单击运行,然后单击仍然继续,然后单击是,然后在对话框中.
- 然后填写这些详细信息:
- 现在转到构建类型并遵循以下图像:
- 然后单击确定.
- Go to the Build option.
- In the options, choose Select Build Variant.
- Then in the left corner, a dialog will open, from there Change Build Variant from debug to release.
- Click on run, then this dialog will open:
- Click on Run, next click on Continue Anyway, then click on Yes then in the dialog .
- Then fill these details:
- Now go to Build Types and follow this image:
- And click ok.
Now when you run the commands to get the hash through AppSignatureHelper Class, that key will be your production key.
这篇关于为什么SMS Retriever API在发布模式下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!