/* Add the permission in manifest file */
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
/* Add the RxPermission permission into gradle file */
implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.1@aar'
implementation 'io.reactivex.rxjava2:rxjava:2.0.5'
/* First call the checkAndRequestPermissions() function from oncreate() method in activity */
private boolean checkAndRequestPermissions() {
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions.request(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.READ_SMS)
.subscribe(new Observer<Boolean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Boolean aBoolean) {
if (aBoolean) {
SMSListener.bindListener(new Common.OTPListener() {
@Override
public void onOTPReceived(String extractedOTP) {
if (flag) {
String[] splited2 = extractedOTP.split(" ");
//Log.d("TAG", "onReceive4: "+splited2[1]);
setdata(splited2[1].trim());
flag = false;
}
}
});
} else {
Toast.makeText(getApplicationContext(), "permission_request_denied",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
return true;
}
/*Added this function into the activity*/
@Override
protected void onDestroy() {
SMSListener.unbindListener();
super.onDestroy();
}
public void setdata(String OTP){
Log.d("TAG", "setdata:"+OTP);
//String[] splited = OTP.split("");
String[] splited = OTP.split("(?!^)");
Log.d("TAG", "onReceive4: "+splited[0]);
Log.d("TAG", "onReceive4: "+splited[1]);
Log.d("TAG", "onReceive4: "+splited[2]);
Log.d("TAG", "onReceive4: "+splited[3]);
}
/* Add the interface */
public interface Common {
interface OTPListener {
void onOTPReceived(String otp);
}
}
/* Create the another class SMSListener that extends the BroadcastReceiver */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SMSListener extends BroadcastReceiver {
private static Common.OTPListener mListener; // this listener will do the magic of throwing the extracted OTP to all the bound views.
@Override
public void onReceive(Context context, Intent intent) {
// this function is trigged when each time a new SMS is received on device.
Bundle data = intent.getExtras();
SmsMessage[] smsm = null;
String sms_str ="";
Object[] pdus = new Object[0];
if (data != null) {
pdus = (Object[]) data.get("pdus");
}
if (pdus != null) {
for (Object pdu : pdus) { // loop through and pick up the SMS of interest
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
// your custom logic to filter and extract the OTP from relevant SMS - with regex or any other way.
//Log.d("TAG", "onReceive: "+smsMessage.toString());
smsm = new SmsMessage[pdus.length];
for (int i=0; i<smsm.length; i++) {
smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sms_str += "\r\nMessage: " + smsm[i].getMessageBody() + "\r\n";
}
//Log.d("TAG", "onReceive: "+sms_str);
if (mListener!=null)
mListener.onOTPReceived(sms_str);
break;
}
}
}
public static void bindListener(Common.OTPListener listener) {
mListener = listener;
}
public static void unbindListener() {
mListener = null;
}
}
Get ready SMS PHP API or Bulk SMS API PHP Gateway Integration Source Code/Script to Integrate into your website, software or web application. MsgClub PHP APIs helps to send verification code or OTP, Transactional & Promotional SMS from your PHP based application automatically.
ReplyDeletebulk SMS API