Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMSManager returns RESULT_ERROR_GENERIC_FAILURE despite trying everything

I am trying to create a feature in my app which can send SMS to multiple contacts in background. I am using SMS Manager API to do the same. However, everytime RESULT_ERROR_GENERIC_FAILURE returns. I read lot of similar posts on stack overflow and tried all the solutions but none of them seems to work for me. To be specific, I have tried following:

  1. Change phone number format to "+91xxxxxxxxxx", "9xxxxxxxxx", "91xxxxxxxxxx"
  2. Checked the SIM balance
  3. SMS Service centre number is also valid
  4. SMS Message is well within 160 characters
  5. Delay sending multiple SMS

Here is how I am sending SMS:

for (int index = 0; index < contactsList.size(); index++) {
            final String phonenumber = contactsList.get(index).getPhone().substring(1);

            if (index == 0) {
                sendSMS(phonenumber.trim(), sms, contactsList);
            } else {
                new Handler().postDelayed(new Runnable() {
                    public void run() {

                        sendSMS(phonenumber.trim(), sms, contactsList);
                    }
                }, 1000 * 20);
            }
        }

Here is sendSMS method:

private void sendSMS(String phoneNumber, final String message, final List<MyContactsActivity.ContactsRow> list) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(
                SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
                new Intent(DELIVERED), 0);

        // ---when the SMS has been sent---
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        ContentValues values = new ContentValues();
                        for (int i = 0; i < list.size() - 1; i++) {
                            values.put("address", list.get(i).getPhone());
                            values.put("body", message);
                        }
                        context.getContentResolver().insert(
                                Uri.parse("content://sms/sent"), values);
                        Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "1");
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "2");
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "3");
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "4");
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "5");
                        break;
                }
            }
        }, new IntentFilter(SENT));

        // ---when the SMS has been delivered---
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "6");
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
                        Log.e("Panic", "7");
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }

Here are my relevant manifest permissions:

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />

NOTE: I am testing my app on real device running Android 6. I have checked runtime permissions as well as they are enabled.

Can anyone please tell me what I am doing wrong??

like image 532
Vaibhav Agarwal Avatar asked Oct 08 '16 12:10

Vaibhav Agarwal


3 Answers

I have found the solution to this problem. In case of dual sim phones, there is an option which specifies from which SIM to send SMS. If option has been set to "Ask everytime", generic failure occurs. Solution is to set this option to either of the SIM card. However, I have not yet found programmatic way to do that.

like image 165
Vaibhav Agarwal Avatar answered Sep 28 '22 09:09

Vaibhav Agarwal


It is maybe due to a large message. Android supports multipart sms for long messages, Try using multipart sms.

SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNo, null, parts, null, null);
like image 37
AtifSayings Avatar answered Sep 28 '22 10:09

AtifSayings


(Solution for Flutter)

This solved issue with sending long SMS messages.

In your pubspec.yaml put this:

  sms_maintained:
    git:
      url: https://github.com/andreimc/flutter_sms.git

I found it here: https://github.com/geordyvcErasmus/flutter_sms/pull/15

like image 45
SardorbekR Avatar answered Sep 28 '22 10:09

SardorbekR