为什么 SMS Retriever API 在发布模式下不起作用? [英] Why SMS Retriever API don't work in release mode?
问题描述
我已经在 google 教程中实现了 SMS Retriever API
,并且在我的调试 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.
我更改了用 AppSignatureHelper 与调试模式不同的发布模式.在调试工作和版本号中.
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);
}
}
});
}
推荐答案
首先下载您的应用签名证书 .der 文件,然后通过此命令转换为 .jks 文件
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
然后创建新的 .jks 文件
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
然后创建哈希字符串,它将在 Play 商店应用程序上运行.
then create hash string and it will work on play store app.
这篇关于为什么 SMS Retriever API 在发布模式下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!