SMS

SMS Supported

You can check whether the current user can send sms using the isSMSSupported call.

AIR
if (Share.service.sms.isSMSSupported)
{
// You should be able to send an sms
}
Unity
if (Share.Instance.SMS.IsSMSSupported)
{
// You should be able to send an sms
}

Sending an SMS with UI

The example below shows how to send an SMS using the native UI:

AIR
if (Share.service.sms.isSMSSupported)
{
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_CANCELLED, smsEventHandler );
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_SENT, smsEventHandler );
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_SENT_ERROR, smsEventHandler );
var sms:SMS = new SMS();
sms.address = "0444444444";
sms.message = "Sending an SMS with the distriqt Message ANE";
Share.service.sms.sendSMSWithUI( sms, false );
}
function smsEventHandler( event:SMSEvent ):void
{
trace( event.type +"::"+ event.details + "::"+event.sms.toString() );
}
Unity
if (Share.Instance.SMS.IsSMSSupported)
{
Share.Instance.SMS.OnSMSSent += SMS_OnSMSSent;
Share.Instance.SMS.OnSMSSentError += SMS_OnSMSSentError;
Share.Instance.SMS.OnSMSCancelled += SMS_OnSMSCancelled;
SMS sms = new SMS();
sms.address = "0444444444";
sms.message = "Sending an SMS with the distriqt plugin";
Share.Instance.SMS.SendSMSWithUI( sms );
}
private function smsEventHandler( event:SMSEvent ):void
{
trace( event.type +"::"+ event.details + "::"+event.sms.toString() );
}
private void SMS_OnSMSCancelled(SMSEvent e)
{
Debug.Log("SMS_OnSMSCancelled");
}
private void SMS_OnSMSSentError(SMSEvent e)
{
Debug.Log("SMS_OnSMSSentError");
}
private void SMS_OnSMSSent(SMSEvent e)
{
Debug.Log("SMS_OnSMSSent");
}

Android Advanced SMS operations

On Android you can request permission to directly send and receive SMS messages without user interaction.

Manifest Additions

The extension requires a few additions to the manifest to be able to start certain activities and to get permission to send and receive SMS.

These are optional and so should be added manually currently.

<manifest android:installLocation="auto">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<!-- To access SIM subscriptions -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application>
<!-- TO RECEIVE SMS -->
<receiver android:name="com.distriqt.extension.share.receivers.SMSReceiver" android:exported="true" >
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>

Requesting Authorisation

Firstly you must request authorisation to send and receive messages.

AIR

On Android these permissions are listed through the manifest additions. On older versions of Android these permissions are accepted when the user installs the application. More modern versions (Marshmallow 6 [v23]+) require that you request the permissions similar to iOS. You will still need to list them in your manifest and then follow the same code below as for iOS, except that on Android you will be able to ask multiple times. You should respect the SHOULD_EXPLAIN status by displaying additional information to your user about why you require this functionality.

The following code will work across both platforms:

Share.service.sms.addEventListener( AuthorisationEvent.CHANGED, authorisationStatus_changedHandler );
switch (Share.service.sms.authorisationStatus())
{
case AuthorisationStatus.SHOULD_EXPLAIN:
case AuthorisationStatus.NOT_DETERMINED:
// REQUEST ACCESS: This will display the permission dialog
Share.service.sms.requestAuthorisation();
return;
case AuthorisationStatus.DENIED:
case AuthorisationStatus.UNKNOWN:
case AuthorisationStatus.RESTRICTED:
// ACCESS DENIED: You should inform your user appropriately
return;
case AuthorisationStatus.AUTHORISED:
// AUTHORISED: SMS will be available
break;
}
function authorisationStatus_changedHandler( event:AuthorisationEvent ):void
{
trace( "authorisationStatus_changedHandler: "+event.status );
}

Unity

You can do this using the normal Permission process through Unity, eg to check whether you have permission to send sms directly:

if (Permission.HasUserAuthorizedPermission("android.permission.SEND_SMS"))
{
//
}

More information here: https://docs.unity3d.com/Manual/android-RequestingPermissions.html

Sending an SMS

Once you have authorisation, sending an SMS is a simple matter of calling sendSMS:

AIR
if (Share.service.sms.isSMSSupported)
{
var sms:SMS = new SMS();
sms.address = "0444444444";
sms.message = "Testing Message ANE";
Share.service.sms.sendSMS( sms );
}
Unity
if (Share.service.sms.isSMSSupported)
{
SMS sms = new SMS();
sms.address = "0444444444";
sms.message = "Sending an SMS with the distriqt plugin";
Share.Instance.SMS.SendSMS( sms );
}

Subscription Info

In some devices there are multiple SIM cards and you may wish to specify the subscription to use to send the SMS.

Firstly you must add the additional permission to READ_PHONE_STATE to be able to access the subscription information.

Then call getSubscriptions() to retrieve an array of SubscriptionInfo objects representing the different sims.

AIR
var subs:Array = Share.service.sms.getSubscriptions();
for each (var sub:SubscriptionInfo in subs)
{
trace( "SIM: ["+sub.id+"] " + sub.displayName + "/"+sub.carrierName);
}
Unity
SubscriptionInfo[] subs = Share.Instance.SMS.GetSubscriptions();
foreach (SubscriptionInfo sub in subs)
{
Debug.Loh( "SIM: ["+sub.id+"] " + sub.displayName + "/"+sub.carrierName);
}

When sending an SMS you can specify the subscription id to use to send the SMS:

AIR
Share.service.sms.sendSMS( sms, sub.id );
Unity
Share.Instance.SMS.SendSMS( sms, sub.id );

Note: This is only supported on Android API v22+. If it is not supported (or if you haven't requested the additional permission) getSubscriptions() will return an empty array and the default sim will be used to send messages.

Events

You can listen for several events, as defined in the SMSEvent class, see the documentation in that class for more information on the events.

AIR
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_CANCELLED, smsEventHandler );
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_DELIVERED, smsEventHandler );
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_RECEIVED, smsEventHandler );
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_SENT, smsEventHandler );
Share.service.sms.addEventListener( SMSEvent.MESSAGE_SMS_SENT_ERROR, smsEventHandler );
function smsEventHandler( event:SMSEvent ):void
{
trace( event.type +"::"+ event.details + "::"+event.sms.toString() );
}