PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] How to find out which sms has not been delivered or sent?

 July 12, 2022     android, java, message, report, sms     No comments   

Issue

I have following code and I want to know which sms has been delivered or not. My code sends 1-5 sms every 30 seconds, so when toast "sms not delivered" appears, I do not know which one was not delivered. I don't know if this is the right way to do this but It was the most common solution to this problem

public boolean sendSMS(String id, String num, String msg) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

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

    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

    registerReceiver(deliveryBroadcastReciever, new 
 IntentFilter(DELIVERED));
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage("xxxxxxxx", null, msg, sentPI, deliveredPI);

    Log.e("Message Sent", num + "  " + msg + "  " + id);

    return true;
}


class DeliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {


        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "sms delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "sms not delivered",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }
}

class SentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "sms sent", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", 
      Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }

I'm sorry for my bad English.

EDIT:

 public boolean sendSMS(String id, String num, String msg) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

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

    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

    registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));

    smsManager.sendTextMessage("+xxxxxxxxxx", null, msg, sentPI, deliveredPI);

    Log.e("Message Sent", num + "  " + msg + "  " + id);

    return true;
}


public class DeliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null){
                final Object[] bObject = (Object[]) bundle.get("pdus");

                for (int i = 0; i < bObject.length; i++){

                    SmsMessage current = SmsMessage.createFromPdu((byte[]) bObject[i]);
                    String phoneNum = current.getDisplayOriginatingAddress();
                }
            }
        }catch (Exception e){
            Log.e("Deliver Reciever",e.toString());
        }
    }
}

Debug screenshot: http://imgur.com/a/GulgP


Solution

Firstly, make your SmSManager a static class object. Then you can get the current phone number/phone numbers from the PDU that is hiddent inside intent object.

final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    String phone = "";
    SmsMessage smsMessage;

    try {

        if (bundle != null) {

            final Object[] pdu = (Object[]) bundle.get("pdu");

            for (int i = 0; i < pdu.length; i++) {

                 smsMessage = SmsMessage.createFromPdu((byte[]) pdu[i]);
                 phone = current.getDisplayOriginatingAddress();
            }
        }
    }
}


Answered By - Chris Maggiulli
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing