Skip to main content

SMS

SMS Supported

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

if (Share.service.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:

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() );
}

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.

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 );
}

Sending an SMS

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

if (Share.service.sms.isSMSSupported)
{
var sms:SMS = new SMS();
sms.address = "0444444444";
sms.message = "Testing Message ANE";

Share.service.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.

var subs:Array = Share.service.sms.getSubscriptions();
for each (var sub:SubscriptionInfo in subs)
{
trace( "SIM: ["+sub.id+"] " + sub.displayName + "/"+sub.carrierName);
}

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

Share.service.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.

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() );
}