Hello
When sending SMS messages and the phone number is not valid, how can we trap the result from the phone where it is 'Failed to Send'.
You can try this on your mobile device by send an SMS to 111111111111111111111, we know in this example is the number is not valid and your SMS app will faile to send it.
This is the code I use for sending SMS messages.
Regards
John.
When sending SMS messages and the phone number is not valid, how can we trap the result from the phone where it is 'Failed to Send'.
You can try this on your mobile device by send an SMS to 111111111111111111111, we know in this example is the number is not valid and your SMS app will faile to send it.
This is the code I use for sending SMS messages.
B4X:
Sub SendLargeSMS(Destination As String, Message As String)
Dim Extra As Map, id As String
mod_functions.writelog("svc_sms(), SendLargeSMS, Message Length = " & Message.Length)
id = DateTime.now
Extra.Initialize
Extra.Put("message_id", id )
Dim tM As String = DateTime.Now
Extra.Put("message_time", tM)
smsPartsACK = 0
smsParts = NativeMe.RunMethod("Send3", Array(Destination, Message, Extra, True, False))
If smsParts = 0 Then
smsParts = 1
End If
' // allow a time out of 10 seconds * num of parts for a small message
messageSendTimeout = DateTime.Now + (DateTime.TicksPerSecond * (smsParts *10))
mod_functions.writelog("svc_sms(), SendLargeSMS, PARTS " & smsParts)
End Sub
#If JAVA
import java.util.Random;
import java.util.ArrayList;
import java.util.Map;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
import android.telephony.SmsManager;
public static int Send3(String PhoneNumber, String Text, Map<String, String> Extra, boolean ReceiveSentNotification, boolean ReceiveDeliveredNotification) {
SmsManager sm = SmsManager.getDefault();
/* Generate a unique requestCode for each Intent */
Random randomNo = new Random();
int requestCode = (int)(System.currentTimeMillis()/1000) + randomNo.nextInt(1000);
int numParts = 0;
Intent mSendIntent = new Intent("b4a.smssent");
mSendIntent.putExtra("phone", PhoneNumber);
// Add additional Extra Data
for (Map.Entry<String, String> entry : Extra.entrySet()) {
mSendIntent.putExtra(entry.getKey(), entry.getValue());
}
PendingIntent sentIntent = ReceiveSentNotification ? PendingIntent.getBroadcast(BA.applicationContext, requestCode, mSendIntent, PendingIntent.FLAG_UPDATE_CURRENT) : null;
Intent mDeliveryIntent = new Intent("b4a.smsdelivered");
mDeliveryIntent.putExtra("phone", PhoneNumber);
// Add additional Extra Data
for (Map.Entry<String, String> entry : Extra.entrySet()) {
mDeliveryIntent.putExtra(entry.getKey(), entry.getValue());
}
PendingIntent deliveryIntent = ReceiveDeliveredNotification ? PendingIntent.getBroadcast(BA.applicationContext, requestCode, mDeliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT) : null;
ArrayList<String> parts = sm.divideMessage(Text);
numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
if (ReceiveSentNotification) {
sentIntents.add(PendingIntent.getBroadcast(BA.applicationContext, requestCode, mSendIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
if (ReceiveDeliveredNotification) {
deliveryIntents.add(PendingIntent.getBroadcast(BA.applicationContext, requestCode, mDeliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
sm.sendMultipartTextMessage(PhoneNumber, null, parts, ReceiveSentNotification ? sentIntents : null, ReceiveDeliveredNotification ? deliveryIntents : null);
return numParts;
}
#End If
Regards
John.