Android Question Trap SMS 'failed to send' when Phone number is invalid

Jmu5667

Well-Known Member
Licensed User
Longtime User
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.

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.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
I am already using the PE_SmsSentStatus(Success As Boolean, ErrorMessage As String, PhoneNumber As String, Intent As Intent), it never gets fired when the circumstance in post #1 occurs.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Maybe start a timer and if the event isn't raised after 30 seconds, show a message.
Thanks for the reply.

Already done that, the problem I have is that I dont know why the message failed - invalid number, service not available etc. The app in question is an SMS Gateway, and it sends hundreds of messages a day, sometimes thousands.

I wounder is there a different callback that can be done on a failure other than SmsSentStatus

John.
 
Upvote 0
Top