9

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());
    }
}
Taufik Nur Rahmanda
  • 1,862
  • 2
  • 20
  • 36

4 Answers4

2

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.

Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22
  • but AFAIK the popup dialog to ask user whether to "copy" or "view messsage" when message arrived is brought up by phone/system (by it's sms application), because the UI is differ from the application. correct me if I'm wrong. – Taufik Nur Rahmanda May 22 '18 at 23:50
  • The UI is different for different apps. Its not related to the operating system but as most of the sms apps are designed by the phone manufacturer themselves, they may use somethings which are not available to other apps. Anyways there is no public API which you can use to notify any app of this behaviour – Suhaib Roomy May 22 '18 at 23:54
  • From my observation of Google Messages, it is enough to provide the code as the first thing in sms, not sure how paypal does it in your case, but in mine the only way to copy code is when the app provides the code as the first "word" in text. – Krzysztof Krasoń May 13 '21 at 19:00
1

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 .

ehsan_kabiri_33
  • 341
  • 6
  • 13
0

Just you need to put "Code:" and a number after ":"

For example: Hello, your verification code: 123456

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
-1

I know its a long time from this question but I answer :) I think it depends on the phone and OS but by the way the thing that I got is that if you write code like this :

:1234

PS: 1234 is a sample code

if the Mobile supports it, it will show the pop up copy code.

Sarah Maher
  • 790
  • 1
  • 10
  • 21