Search code examples
androidnotificationssmsbroadcastreceiver

Notification for sending sms to multiple recipients - not working


Sorry to begin like this: I am frustrated. I read a lot about of how to do it. Spent on this 2-3 days already and it is still not working.

The object is to send SMS (same message) to multiple recipients. The sending itself works but the notification does not.

I put everything in a loop. The onReceive of the BroadcastReceiver should toast each time a different index (idx) but it does not. It toast the same index (0) for all iterations.

What am I doing wrong?

The code is shown below.

Thanks, AJ

            for(idx = 0; idx < mobileList.length; idx++) {

                toNumber = mobileList[idx];
                sms = message;

                Intent sentActionIntent = new Intent(SENT_ACTION);
                sentActionIntent.putExtra(EXTRA_IDX, idx);
                PendingIntent sentPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, sentActionIntent, 0);

                Intent deliveredActionIntent = new Intent(DELIVERED_ACTION);
                deliveredActionIntent.putExtra(EXTRA_IDX, idx);
                PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, 0, deliveredActionIntents, 0);


            /* Register for SMS send action */
            registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    String result = "";

                    switch (getResultCode()) {

                    case Activity.RESULT_OK:
                        result = "Transmission successful";
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        result = "Transmission failed";
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        result = "Radio off";
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        result = "No PDU defined";
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        result = "No service";
                        break;
                    }

                    SmsActivity.this.unregisterReceiver(this);
                }

            }, new IntentFilter(SENT_ACTION));                  

            /* Register for Delivery event */
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {

                  switch (getResultCode())
                  {
                    case Activity.RESULT_OK:
                      Toast.makeText(getBaseContext(), "SMS delivered " + Integer.toString(arg1.getIntExtra(EXTRA_IDX, -1)),
                        Toast.LENGTH_SHORT).show();
                      break;
                    case Activity.RESULT_CANCELED:
                      Toast.makeText(getBaseContext(), "SMS not delivered",
                        Toast.LENGTH_SHORT).show();
                      break;
                    }

                  SmsActivity.this.unregisterReceiver(this);


                  }


                }, new IntentFilter(DELIVERED_ACTION));                 

                smsManager.sendTextMessage(toNumber, null, sms, sentPendingIntent, deliveredPendingIntent);


            }

Solution

  • PendingIntents can be, and are, reused by the system if certain details are the same as one previously issued. Creating a PendingIntent in which only the included Intent's extras are different does not cause the system to update the original unless you pass a flag to indicate as much.

    You should pass a different request code - the second parameter in getBroadcast() - for each PendingIntent creation, and specify FLAG_UPDATE_CURRENT or FLAG_CANCEL_CURRENT for the last parameter. For example:

    PendingIntent sentPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, idx, sentActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(SmsActivity.this, idx, deliveredActionIntents, PendingIntent.FLAG_UPDATE_CURRENT);
    

    You should also make your Receivers class members, and register them only once before your loop, instead of registering and unregistering a new one for each message.