Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample How to Send SMS in Android using Unity3D and SMS Manager. (without opening SMS Composer)
<?xml version="1.0" encoding="utf-8"?>
<!-- Directory : [Project]/Assets/Plugins/Android/AndroidManifest.xml -->
<!-- Configure according to your android application information. -->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<!-- REQUIRED permission -->
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- Additional -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SMS_RECEIVED" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:theme="@style/UnityThemeSelector"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
using UnityEngine;
public class SendSMS : MonoBehaviour
{
AndroidJavaObject currentActivity;
public void Send(string phone)
{
if (Application.platform == RuntimePlatform.Android)
{
RunAndroidUiThread();
}
}
void RunAndroidUiThread()
{
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(SendProcess));
}
void SendProcess()
{
Debug.Log("Running on UI thread");
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
// SMS Information
string phone = "08888888888";
string text = "Hello World. This SMS is sent using Android SMS Manager.";
string alert;
try
{
// SMS Manager
AndroidJavaClass SMSManagerClass = new AndroidJavaClass("android.telephony.SmsManager");
AndroidJavaObject SMSManagerObject = SMSManagerClass.CallStatic<AndroidJavaObject>("getDefault");
SMSManagerObject.Call("sendTextMessage", phone, null, text, null, null);
alert = "Message sent successfully.";
}
catch (System.Exception e)
{
Debug.Log("Error : " + e.StackTrace.ToString());
alert = "Error has been Occurred. Fail to send message.";
}
// Show Toast
AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", alert);
AndroidJavaObject toast = Toast.CallStatic<AndroidJavaObject>("makeText", context, javaString, Toast.GetStatic<int>("LENGTH_SHORT"));
toast.Call("show");
}
}
@BahmanAries

This comment has been minimized.

Copy link

@BahmanAries BahmanAries commented Feb 5, 2017

This works and this is awesome! thank you.

@Veria70

This comment has been minimized.

Copy link

@Veria70 Veria70 commented Feb 16, 2017

Can i use sms manager to receive massages?

@Dibbie

This comment has been minimized.

Copy link

@Dibbie Dibbie commented Jul 17, 2017

Oh wow, after I figured out how to implement this, it worked amazingly! My friend is probably annoyed with all the "test" messages, but it works, so I dont care : P - awesome work on putting all this together in a simple and easy-to-implement way!

@christianpugger

This comment has been minimized.

Copy link

@christianpugger christianpugger commented Oct 16, 2017

help, i do not understand how to implement it, i always have the error "Error has been Occurred. Fail to send message"

@shubhampandeymv

This comment has been minimized.

Copy link

@shubhampandeymv shubhampandeymv commented Nov 22, 2017

I am trying to implement automatic OTP reader in UNITY for android . Received OPT should be read automatically without viewing it.

@eagleeyez71

This comment has been minimized.

Copy link

@eagleeyez71 eagleeyez71 commented Feb 19, 2018

Very good, it worked like a charm. now please, please I need to be able to read incomming sms in Unity for my android app. It is so that partially sighted people get the text reading as I have implimented it with TTS, STT, calling contact and with this sending SMS. I just need to be able to read out the sms now. This needs to be achivable in Unity.

@VicToMeyeZR

This comment has been minimized.

Copy link

@VicToMeyeZR VicToMeyeZR commented May 12, 2018

Just get an Android error "Error has been Occurred. Fail to send message".

@aygin

This comment has been minimized.

Copy link

@aygin aygin commented Jul 12, 2018

I have the same error as VicToMeyeZR.

@aygin

This comment has been minimized.

Copy link

@aygin aygin commented Jul 12, 2018

I found it!

  1. you should use the directory in line 2 for AndroidManifest.xml
  2. change line 6 (package information) of AndroidManifest file according to your package
@RicardoMen

This comment has been minimized.

Copy link

@RicardoMen RicardoMen commented Apr 28, 2019

Doesn't work for me, can you give me a example? y put the number, and send() at boton fuction, but nothing happend.

@Ahere

This comment has been minimized.

Copy link

@Ahere Ahere commented Oct 17, 2019

Hi. Thanks for making this, I keep getting one error though. My game always exits after the message is sent. Could there be something I am doing wrong?

@zhusir0605

This comment has been minimized.

Copy link

@zhusir0605 zhusir0605 commented Nov 13, 2019

Works fine on Unity 2017.4.27f1 (64-bit), but crashed on unity 2019.2.11f1

@AliBinNaeem

This comment has been minimized.

Copy link

@AliBinNaeem AliBinNaeem commented Apr 5, 2020

Just get an Android error "Error has been Occurred. Fail to send message".

Same here

@AliBinNaeem

This comment has been minimized.

Copy link

@AliBinNaeem AliBinNaeem commented Apr 5, 2020

help, i do not understand how to implement it, i always have the error "Error has been Occurred. Fail to send message"

Check your xml path

@axel-martinez

This comment has been minimized.

Copy link

@axel-martinez axel-martinez commented Jul 22, 2020

hola
necesito ayuda con este código, ya que lo ocupo para un proyecto

@Cunibon

This comment has been minimized.

Copy link

@Cunibon Cunibon commented Sep 28, 2020

Hey im trying to implement this code but it wont work, could it be that this is impossible in newer android versions?

@rmdwirizki

This comment has been minimized.

Copy link
Owner Author

@rmdwirizki rmdwirizki commented Sep 28, 2020

@Cunibon I'm sorry to say that I don't know, android often updating their spec and API on the newer version, especially permission issue.. I haven't touched Unity and Android in the last couple of years, I'll check and let let you know if I have the time

@Cunibon

This comment has been minimized.

Copy link

@Cunibon Cunibon commented Sep 29, 2020

@rmdwirizki that would be great. I've been trying to get an answer on reddit or stackoverflow for months now didnt expect to actually get an answer here 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.