Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 975

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??

Upvotes: 4

Views: 5141

Answers (7)

Trash
Trash

Reputation: 15

i was using "~" in my message. guess one must sanitize string before sending

Upvotes: 0

Qadir Hussain
Qadir Hussain

Reputation: 8856

I got the same problem with same above code

I tried restarted my phone once and try again it worked.

Upvotes: 0

SardorbekR
SardorbekR

Reputation: 1758

(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

Upvotes: 2

Iraha Services
Iraha Services

Reputation: 11

For me the problem was the content of the message, there was special characters (like /r /n)

Upvotes: 0

AtifSayings
AtifSayings

Reputation: 844

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);

Upvotes: 2

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 975

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.

Upvotes: 5

Ganesh Pokale
Ganesh Pokale

Reputation: 1594

Your code is correct. check you have active SIM card and having balance in it. Because Generic failure is response from your service provider.

Upvotes: 0

Related Questions