A simple demo for receive SMS with Android plugin in Unity
Unity 2019.4.12f1
Android Studio 4.0.1
Android Emulator
CPU/ABI: Google Play Intel Atom (x86)
Target: google_apis_playstore [Google Play] (API level 30)
-
SmsListener.java
is the main componment to receive SMS message and pass the result to unity side. -
Customizing the
Android manifest
:Permission to receive and read SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" />
Specify the broadcast receiver:
<receiver android:name="com.ming.hello.SmsListener" android:enabled="true"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>
- Place a gameobgect named
UnitySMSReceiver
to receive message from android plugin side. - Attach a script to the gameobject and declare a method to receive message send from Android plugin
public void OnSMSReceive (string message)
- From android side, invoke this method through UnityPlayer API
UnityPlayer.UnitySendMessage(gameobject_name, method_name, message);
- Clone this repository
- Use Unity to open
UnityReceieveSMSDemo
directory - Switch to Android platform
- Build or export to create apk and run
- Android app permission: This demo project has not handle the SMS permission yet. If you can't receive any message, check app permission first.
- How to grant SMS permission?