我们必须编写基于OTP的身份验证代码.我看过一些应用程序,比如我的银行应用程序,当它发送OTP时,它会立即快速弹出刚刚到达的短信,这样我就可以在不离开应用程序的情况下看到OTP.我只记住数字,关闭弹出窗口,继续在该应用程序内登录.
@H_502_7@解决方法
他们是怎么做到的?是否有一些我应该关注的iOS / Android规范,它允许我们类似地弹出OTP而无需用户必须转到SMS屏幕,然后回到我们的应用程序?谢谢!
编辑:我有非常有用的Android建议.现在正在寻找这些建议的iOS变体.了解iOS有更严格的沙盒限制,所以“听众”可能会更复杂?
这是一步一步的描述,以实现您的要求
>在AndroidManifest中声明接收器
<receiver android:name=".IncomingSms"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>
2在AndroidManifest中提供阅读短信权限
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> <uses-permission android:name="android.permission.READ_SMS" />
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidexample.broadcastreceiver" android:versionCode="1" android:versionName="1.0" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.androidexample.broadcastreceiver.BroadcastNewSms" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> <uses-permission android:name="android.permission.READ_SMS" /> </manifest>
public class IncomingSms extends BroadcastReceiver { // Get the object of SmsManager final SmsManager sms = SmsManager.getDefault(); public void onReceive(Context context,Intent intent) { // Retrieves a map of extended data from the intent. final Bundle bundle = intent.getExtras(); try { if (bundle != null) { final Object[] pdusObj = (Object[]) bundle.get("pdus"); for (int i = 0; i < pdusObj.length; i++) { SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); String phoneNumber = currentMessage.getDisplayOriginatingAddress(); String senderNum = phoneNumber; String message = currentMessage.getDisplayMessageBody(); Log.i("SmsReceiver","senderNum: "+ senderNum + "; message: " + message); // Show Alert int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context,"senderNum: "+ senderNum + ",message: " + message,duration); toast.show(); } // end for loop } // bundle is null } catch (Exception e) { Log.e("SmsReceiver","Exception smsReceiver" +e); } } }