androidandroid-pendingintentsmsmanager

PendingIntent in SmsManager


In the messaging program I made, I used pending intent to receive the message sending and delivery report. When I send several messages and wait for the delivery report, the program does not recognize which message the received pending intent belongs to. My code is: sentSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_SENT), 0); deliverSMS = PendingIntent.getBroadcast(this, msgId(), new Intent(SMS_DELIVERED), 0); According to what I searched for, I have to use the request code parameter for this, for each message I have assigned a request code **(the ID of each message from the database) **in pending intent, but I don't know how to receive it in on receive.

thanks

Also i tried this:

Intent deliveryIntent = new Intent();
          deliveryIntent.putExtra("id", msgId());
          deliverSMS = PendingIntent.getBroadcast(this, msgId(), deliveryIntent, 0);
          //************
          Intent sendIntent = new Intent();
         sendIntent.putExtra("id", msgId ());
          sentSMS = PendingIntent.getBroadcast(this, msgId(), sendIntent, 0);
         

But I did not receive anything in extra

TThanks 🙏


Solution

  • When you create the PendingIntent, you need to specify PendingIntent.UPDATE_CURRENT as the last parameter of the getBroadcast() call. This will ensure that each PendingIntent has the correct "id".

    In onReceive() you should get the broadcast Intent and it should contain the "extra" named "id" with the message ID.

    However, if you create the Intent without an ACTION and without a component or package name, your onReceive() will never get called. Please edit your question and show how you have set up your BroadcastReceiver.