I am developing an application in which i need to send 100+ of messages. After going through few threads i came to know there is limitation on sending messages like 100 messages can be send in an hour. To do so i divide my recipient list into chunks and place delay of 5 seconds between each chunk and 3 seconds delay in every message. The delay between chunks increase after every chunk and when it gets to 100 seconds it will reset to 5 seconds. After that it worked OK for 50 messages but when i raise recipient list it causing issues some messages didn't go at first place and shown as error messages in native.
Is there any standard way to achieve this i may need to send 100+ messages , how can i send multiple messages without any failure at once. If i need to place delay what should be the appropriate delay between chunks or messages.
Thanks in advance.
private final int MAX_SMS_IN_ONE_TIME = 10;
private final int DELAY_BETWEEN_CHUNKS = 5000;
public void sendMessage(arguments){
// Send long messages in chunk of 20 messages and put gap of increasing 5 seconds till 50 seconds and then reset.
final Iterator iterator = messageChunks.iterator();
new Thread(new Runnable() {
@Override
public void run(){
int interval =1;
while (iterator.hasNext()) {
for (final Contact contact :
(List<Contact>) iterator.next()) {
sendSMS(body, contact.getmMobileNumbers().get(0));
App.trackEvent("Message", "Sent", "Messages from our sms app");
}
}
try {
Log.i("chunk", "chunk # " + interval + " delay is " + DELAY_BETWEEN_CHUNKS);
Thread.sleep(DELAY_BETWEEN_CHUNKS * interval);
interval++;
if (interval == 10) {
interval = 1;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void sendSMS(final String message, final String phoneNo) {
try {
String SENT = "com.ebryx.smscustommessagegeneration"+""+System.currentTimeMillis()+""+((int)this.getmMessageId());
Intent intentMessageASendStatus = new Intent(SENT);
final PendingIntent pi = PendingIntent.getBroadcast(App.getContext(), ((int)this.getmMessageId()),
intentMessageASendStatus, PendingIntent.FLAG_CANCEL_CURRENT);
final ArrayList<PendingIntent> sentPI = new ArrayList<PendingIntent>(){{add(pi);}};
App.getContext().registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Log.i("tag","sent successfully ");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.i("tag","Generic Failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.i("tag","No service failure");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.i("tag","Airplane mode failure");
break;
}
}
}, new IntentFilter(SENT));
final SmsManager smsManager = SmsManager.getDefault();
final ArrayList<String> parts = smsManager.divideMessage(message);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
smsManager.sendMultipartTextMessage(phoneNo, null, parts, sentPI, null);
}}, 3000);
}
} catch (Exception e) {
e.printStackTrace();
}
}