1

I need to implement a small text UI to show OTP MSM messages from inbox in Unity.

Does anyone know how to check or send SMS messages in Unity without opening the default SMS app?

I found the link below for sending the message without launching default SMS app. I want to read the SMS message in order to auto fill the OTP for the registration process. Is it possible with Unity?

https://gist.github.com/rmdwirizki/87f9e68c7ef6ef809a777eb25f12c3b2

check or reading sms from inbox in unity

https://gist.github.com/taesiri/7431660

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
  • On Android platform, I believe it can be done with writing your own plugin with Java. [Building and using plug-ins for Android](https://docs.unity3d.com/Manual/PluginsForAndroid.html) I am willing to create a simple demo project to show how to do that. But it might takes some time. – ming060 Dec 06 '18 at 15:19
  • On iOS you cannot read the user's messages. It would be a privacy violation. In iOS 12 if you set a text field type to "password" then iOS will suggest an incoming OTP as an autofill option if one is detected. – Paulw11 Dec 06 '18 at 21:05
  • Thank you @ming060. I will try to develop using the "Building and using plug-ins for Android" and i will let u know if time permits show me some sample or guide. – keerthikaitmit Dec 07 '18 at 04:28

1 Answers1

3

On Android platform:

Demo Project here

  1. Create an AAR plug-ins and Android Libraries. (You can either chose jar or aar to implement the plugin, the only difference is that aar plugin contains Android resources. In this case, we need to add permission into AndroidManifiest.xml which can be put inside aar and be merge by Unity automatically later)

    Follow this instruction Create an Android library.

    In the aar plugin, implement a BroadcastReceiver that handles SMS receive intent.

    SmsListener.java

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.SmsMessage;
    
    import com.unity3d.player.UnityPlayer;
    
    public class SmsListener extends BroadcastReceiver {
        private final String UnityCallbackObject = "UnitySMSReceiver";
        private final String UnityCallbackMethod = "OnSMSReceive";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            StringBuilder builder = new StringBuilder();
    
            Object[] objects = (Object[]) intent.getExtras().get("pdus");
            SmsMessage message = null;
            for (int i = 0; i < objects.length; i++) {
                message = SmsMessage.createFromPdu((byte[]) objects[i]);
                builder.append(message.getDisplayMessageBody());
            }
            UnityPlayer.UnitySendMessage(UnityCallbackObject, UnityCallbackMethod, builder.toString());
        }
    }
    

    AndroidManifiest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="your.package.name.here">
    
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
    
        <application>
            <receiver android:name="your.package.name.here.SmsListener" android:enabled="true">
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
        </application>
    </manifest>
    
  2. In Unity side, put the generated aar file into Assets/Plugins/Android. Create an gameObject named UnitySMSReceiver and attached a script implement function OnSMSReceive in the scene.

    public void OnSMSReceive (string message) {
        // do something with your SMS message here.
    }
    

    My Environments:

    Unity 2017.2.0f3
    
    Android Studio 3.2.1
    

References:

Android testing sms from emulator

Android – Listen For Incoming SMS Messages

How can I read SMS messages programmatically in Android

Android: Programmatically receiving SMS messags

Android SMS Receiver not working

Unity3D and aar issue

https://blog.gradle.org/introducing-compile-only-dependencies

ming060
  • 302
  • 3
  • 6
  • Thank you so much @ming060 its working great. I tried to create a new project based on your instruction, It is working good. Thanks. – keerthikaitmit Dec 10 '18 at 08:14
  • But i have one doubt, it is receiving all the sms content. How can i restrict the particular sender sms should receive. I had control the content and got the OTP number alone in the unity side. – keerthikaitmit Dec 10 '18 at 08:17
  • How do you pass the message content from Android side to Unity side now? – ming060 Dec 10 '18 at 09:45
  • I am not passing msg from android to unity. Exactly my need is when user register application with the email and mobile number. We are try to verify the mobile number using the OTP method. Basically here we are sending msg to the concern number through the bulk sms portal from backend. I had trip the string based on my need. and try to display. Thats what i am asking can we read the particular sender sms. – keerthikaitmit Dec 10 '18 at 12:19
  • So, are you using an Android plugin to retrieve all SMS messages on the phone? – ming060 Dec 10 '18 at 12:25
  • Yes but not all msg. For example : "your Mi Account verification code is 068887. This OTP expires in 1 days". From this sms I need to read that OTP alone to auto fill. Need to read the sms from particular recipients. – keerthikaitmit Dec 10 '18 at 12:30
  • Do you only want to get the "068887" code from the message? – ming060 Dec 10 '18 at 12:32
  • Yes @ming060 absolutely. – keerthikaitmit Dec 10 '18 at 12:41
  • If your OTP SMS message satisfies certain pattern, use `Regular expression` to filter the OTP code is the first way I think of. – ming060 Dec 10 '18 at 12:45
  • Sure i will try that way. But my concern is to retrieve msg from the particular sender not all msg. We usually get OTP msg from the bank sites also or other login or forget pwd method. – keerthikaitmit Dec 10 '18 at 13:41
  • In my above demo project, I can get the sender of the SMS message. I don't know whether you can get that info from your plugin. – ming060 Dec 10 '18 at 14:18
  • I will check @ming060. – keerthikaitmit Dec 11 '18 at 11:12
  • @keerthikaitmit Were you successful in retrieving msg from the particular sender? If so can you please help me with the same? – Sathish Jun 27 '19 at 11:59
  • is this still working? can't find a way to make it receive an sms – Skin_phil Oct 08 '20 at 17:26
  • Hi @Skin_phil, just fixed the problem. And please make sure to grant SMS permission manually. – ming060 Oct 22 '20 at 07:56
  • Thank you, yeah the problem was actually getting the permissions, after that no problems – Skin_phil Oct 22 '20 at 15:06