Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send SMS containing verification code with a "copy code" button

I've managed to send SMS containing verification code from my app using SmsManager and SmsReceiver. But it's just like a normal SMS message. I wonder if I can tell to device that the SMS is containing code, thus user's device will automatically open popup dialog to ask user wether they will copy the code to clipboard or not.

I know several apps also do that. I was never capture the screenshot of the popup dialog I mean, but I hope you get the idea. (the popup is from android/system, not from apk)

Even the button is shown inside the SMS too:

enter image description here

How can we achieve this?

This is my current code in my activity:

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(val_no_hp, null, "Kode verifikasi aplikasi: " + kode_verifikasi, null, null);

kode_otp_rcv = new SmsReceiver(MainActivity.this);
IntentFilter kode_otp_filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(kode_otp_rcv, kode_otp_filter);

And this is my SmsReceiver class:

public class SmsReceiver extends BroadcastReceiver {
    private MainActivity act;
    private static int PANJANG_KODE_VERIFIKASI = 4;

    public SmsReceiver(MainActivity act) {
        this.act = act;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
        SmsMessage msg = msgs[0];

        String body = msg.getMessageBody();
        String kode = body.substring(body.length() - PANJANG_KODE_VERIFIKASI);

        // currently my app submit verification automatically when SMS arrived
        // act.verifikasiOTP(kode, msg.getOriginatingAddress());
    }
}
like image 978
Taufik Nur Rahmanda Avatar asked May 22 '18 23:05

Taufik Nur Rahmanda


2 Answers

This is not possible, this functionality is not provided by android and is built by application developers themselves, the logic is private and varies from app to app, some apps may recognise some codes, whilst others may not.

like image 103
Suhaib Roomy Avatar answered Oct 05 '22 03:10

Suhaib Roomy


I send my verification SMS like this: code: 123456 Your code from Example.com

The important text is : code: 1111 at the begining

and in android mobiles, automatic copy is enabled .

like image 20
ehsan_kabiri_33 Avatar answered Oct 05 '22 03:10

ehsan_kabiri_33