There is how I send message to multiple recipient:
public void send(List<User> participants,
Func1<User, String> messageTextCallback,
Subscriber<SendingProgress> subscriber) { // TODO: throw error
Observable<SendingProgress> observable = Observable.create(sub -> {
String unequalSmsSentAction = UUID.randomUUID().toString();
context.registerReceiver(new BroadcastReceiver() {
private int totalToSend = participants.size();
private int sentCounter = 0;
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
sub.onNext(
new SendingProgress(totalToSend, ++sentCounter)
);
if(sentCounter == totalToSend) {
sub.onCompleted();
context.unregisterReceiver(this);
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
subscriber.onError(new SendSmsException());
sub.onCompleted();
}
}
}, new IntentFilter(unequalSmsSentAction));
int totalToSend = participants.size();
for(int i = 0; i < totalToSend; i++) {
User participant = participants.get(i);
logger.d(TAG, "sending to: " + participant.getUsername());
PendingIntent sentPi = PendingIntent.getBroadcast(context, 0, new Intent(unequalSmsSentAction), 0);
smsManager.sendTextMessage(
participant.getPhones().get(0),
null,
messageTextCallback.call(participant),
sentPi,
null
);
}
});
observable
.subscribeOn(ioScheduler)
.observeOn(mainThreadScheduler)
.subscribe(subscriber);
}
This code call SmsManager.sendTextMessage(...)
for every User's phone number.
BroadcastReceiver
receives every sent message and notifies subscriber about it. I want to get phone number of SMS recipient inside BroadcastReceiver.onReceive
to pass it through SendingProgress
.
Is there any way to do this? Should I send multiple SMSes synchronously?
Is there any way to do this?
Step #1: Put the information that you want in an extra of your unequalSmsSentAction
Intent
Step #2: Use a unique ID for each PendingIntent
(the second parameter of your getBroadcast()
call, where you have 0
-- replace it with a distinct value for each SMS)
Step #3: Read the information out of extra in the Intent
delivered to your BroadcastReceiver
However, any app can listen for your unequalSmsSentAction
broadcasts. Ideally, set the value of the extra in the Intent
to be something you can use to find the phone number, but that cannot be used by any other app that receives those broadcasts.
Should I send multiple SMSes synchronously?
AFAIK, you cannot send them synchronously.