1

I am sending an sms from within the app and I am able to do so. the problem is the pending intent for broadcast of either send/delivered is not getting called. Do I require to put anything more?

Please check my code here:

Android Manifest

<uses-feature android:name="android.provider.Telephony" android:required="true"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

    <receiver
        android:name=".receivers.SentReceiver">
        <intent-filter>
            <action android:name="sms.SENT" />
        </intent-filter>
    </receiver>

Pending Intent

PendingIntent.getBroadcast(this, messageId, new Intent("sms.SENT"), PendingIntent.FLAG_UPDATE_CURRENT);

Sending the sms

private void sendSms(Message msg, Uri smsUri, int messageId) {
    SmsManager smsManager = SmsManager.getDefault();

    // set up sent and delivered pending intents to be used with message request
    PendingIntent sentPI = view.getSentPendingIntent(smsUri, messageId);
    PendingIntent deliveredPI = view.getDeliveredPendingIntent(smsUri, messageId);
    // divide the long text of sms into parts as it can be more than 160 Char
    ArrayList<String> parts = smsManager.divideMessage(msg.getMsg());
    // so as we need different reports for different sms
    ArrayList<PendingIntent> sPI = new ArrayList<>();
    ArrayList<PendingIntent> dPI = new ArrayList<>();

    for (String part : parts) {
        sPI.add(sentPI);
        dPI.add(deliveredPI);
    }
    // send SMS
    try {
        smsManager.sendMultipartTextMessage(msg.getTitle(), null, parts, sPI, dPI);
    } catch (Exception e) {
        Log.e(TAG, "exception thrown", e);
    }
}

Receiver

public class SentReceiver extends BroadcastReceiver {
private static final String TAG = "SentReceiver";

@Override
public void onReceive(Context context, Intent intent) {
   // debug point doesn't reach here and logs are also not getting print
    String path = intent.getStringExtra(Defaults.IntentKeys.MESSAGE_URI);
    Log.e("reached","sent receiver");
  }
}

I have debugged this and it is not coming here. Do I need to add something else? - My app is default sms App

Cyph3rCod3r
  • 1,978
  • 23
  • 33
  • Which Android version are you testing on? Implicit `Intent`s no longer work for manifest-registered Receivers, as of Oreo. You should be using explicit `Intent`s, regardless; e.g., `new Intent(this, SentReceiver.class).setAction("sms.SENT")`. And remove the ``. Beyond that, do you have the `` element in the right spot in the manifest? That is, between the `` tags, but not inside anything else? – Mike M. Jul 31 '18 at 13:27
  • oreo, but this is an explicit intent right? – Cyph3rCod3r Jul 31 '18 at 13:37
  • Yes I have checked its in the right place in manifest file. – Cyph3rCod3r Jul 31 '18 at 13:38
  • 2
    Nope. `new Intent("sms.SENT")` is an implicit `Intent`, since it only has an action. That's likely why it's not working, then, if you've the `` in the right spot. – Mike M. Jul 31 '18 at 13:41
  • Ok, I have changed it to be like this **new Intent(this, SentReceiver.class).setAction("sms.SENT")** and removed intent filter from manifest. it still doesn't work. Or do I have to register it in activity? if yes what if user left the spot and i need to update status of the sms? – Cyph3rCod3r Jul 31 '18 at 13:45
  • No, you're registering it in the manifest. Registering in the `Activity` would be redundant, and, yeah, it wouldn't work if the `Activity` goes away. Are you testing with a short message? Or a long one that would end up being split? – Mike M. Jul 31 '18 at 14:01
  • 1
    Removing Intent Filter did the JOB! – Cyph3rCod3r Aug 02 '18 at 09:50
  • Should I close the question or maybe you can post an answer? – Cyph3rCod3r Aug 02 '18 at 10:28
  • Oh, I already marked it as a duplicate. Thanks, though. – Mike M. Aug 02 '18 at 10:29

0 Answers0