2

I am developing an SMS/MMS messaging system and I need the app to receive both SMS and MMS messages. It receives SMS messages just fine. I am testing on a real phone (Galaxy S7 Edge running 7.01).

The app does not receive MMS messages. According to this post, my manifest is correct:

    <receiver android:name="com.webnation.text2email.receivers.MMSBroadcastReceiver"
        android:enabled="true"
        android:permission="android.permission.BROADCAST_WAP_PUSH"
        android:exported="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />

            <data android:mimeType="application/vnd.wap.mms-message" />
        </intent-filter>
    </receiver>

However, setting a break point at the first line of the broadcast receiver, the broadcast receiver is never called. I am including all the right permissions.

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="com.android.vending.BILLING" />

My broadcast receiver:

public class MMSBroadcastReceiver extends BroadcastReceiver {

  private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_DELIVER";
  private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";

  // Retrieve MMS
  public void onReceive(Context context, Intent intent) {

    this.context = context; <------- break point set here. 
    String action = intent.getAction();
    String type = intent.getType();
    int typeMessage = -1;
    byte[] pushData;
    GenericPdu pdu = null;
    Toast.makeText(context, "MMS received", Toast.LENGTH_LONG).show();

    if (action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)) {

        Bundle bundle = intent.getExtras();

        Timber.d("bundle " + bundle);
        if (bundle != null) {
            pushData = intent.getByteArrayExtra("data");
            PduParser parser = new PduParser(pushData);
            pdu = parser.parse();
            PduHeaders headers = pdu.getPduHeaders();

            Timber.d("buffer " + pushData);
            String incomingNumber = new String(pushData);
            int indx = incomingNumber.indexOf("/TYPE"); // not used, parse the number in MMUtils.getAddress instead
            if (indx > 0 && (indx - 15) > 0) {
                int newIndx = indx - 15;
                incomingNumber = incomingNumber.substring(newIndx, indx);
                char[] characters = incomingNumber.toCharArray();
                int index = 0;
                for (int i=0;i<characters.length;i++) {
                    if (Character.isDigit(characters[i]) ){


                        index = i;
                        break;
                    }
                }
                //indx = incomingNumber.indexOf("+");
                if (index > 0) {
                    incomingNumber = incomingNumber.substring(index);
                    TelephonyManager tm = (TelephonyManager)context.getSystemService(context.getApplicationContext().TELEPHONY_SERVICE);
                    String countryCode = tm.getNetworkCountryIso().toUpperCase();
                    try {
                        String prefix = CountryToPhonePrefix.prefixFor(countryCode).replace("+", "");
                        int indexOfBaseNumber = incomingNumber.indexOf(prefix);
                        if (indexOfBaseNumber > -1) {

                            if (indexOfBaseNumber == 0) {
                                int lengthPrefix = prefix.length();
                                indexOfBaseNumber = indexOfBaseNumber + lengthPrefix;
                            }
                            incomingNumber = incomingNumber.substring(indexOfBaseNumber);
                        }

                    } catch (IllegalArgumentException ie) {
                        Timber.e(ie);
                    } catch (IndexOutOfBoundsException ie) {
                        Timber.e(ie);;
                    }
                    Timber.d("Mobile Number: " + incomingNumber);
                }
            }

            int transactionId = bundle.getInt("transactionId", -1);
            Timber.d("transactionId " + transactionId);

            int pduType = bundle.getInt("pduType", -1);
            Timber.d("pduType " + pduType);

            byte[] buffer2 = bundle.getByteArray("header");

            if (buffer2 != null) {
                String header = new String(buffer2);
                Timber.d("header " + header);
            }
            List<MMSMessage> textMessages = MMSUtils.getMessagesFrom(context, intent); //meat of onReceive()
            sendMessages(textMessages);

        } else {
            Timber.e("Invalid PUSH data");
        }

    }


}
}

What am I doing wrong?

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • @MikeM. I actually tried with WAP_PUSH_RECEIVED and it didn't work. – Kristy Welsh Apr 12 '18 at 19:22
  • Well, what have you changed since this: https://stackoverflow.com/questions/49039151/trying-to-receive-an-mms-message-android#comment85110035_49039151? – Mike M. Apr 12 '18 at 19:23
  • @MikeM. Absolutely nothing except the WAP_PUSH_RECEIVED. – Kristy Welsh Apr 12 '18 at 19:24
  • I guess I'm not following, here. Are you saying it never worked? Or it doesn't work on this particular device? Or it stopped working when you upgraded versions? Or something else? – Mike M. Apr 12 '18 at 19:27
  • It doesn't work on my device - not sure if it would run on other devices. It worked briefly after adding a correct permission in debug, then did not work when not in debug at all. Then I tried adding WAP_PUSH_DELIVER and it is still not working. – Kristy Welsh Apr 12 '18 at 19:32
  • Also, are you certain the received messages are actually MMS? Might they possibly be [RCS](https://stackoverflow.com/a/38046131)? If so, your Receiver won't fire for those at all. – Mike M. Apr 12 '18 at 19:41
  • Yes, positive they are MMS messages. My messaging app labels them as MMS messages. – Kristy Welsh Apr 12 '18 at 19:43
  • OK, though I wouldn't necessarily trust that blindly. :-) Anyhoo, I've got an S7 on 7.0, so I'll run a few tests later myself, when I get a chance, and let ya know if I find anything useful. – Mike M. Apr 12 '18 at 19:46
  • Yeah, and just FYI, I've offered a bounty on my previous question. Thanks so much!!!! – Kristy Welsh Apr 12 '18 at 19:46
  • Well, I (eventually) had no problem receiving MMS, even when not the default, in both debug and release builds. At first, I had Advanced Messaging (another term for RCS; there are several different names for it) enabled, and I was puzzled for a bit, until I remembered to disable it. After that, it works as expected. Who's your cellular provider? Can you receive when you're the default, with `WAP_PUSH_DELIVER`? Are you _really_ sure those messages are MMS? You might try doing a `ContentResolver#query()` just to make sure. – Mike M. Apr 12 '18 at 21:43
  • Other than that, the only thing I can think of at the moment is to check if your device has additional security stuff that might prevent third-party apps from receiving MMS, though I'd imagine that would block SMS, as well. – Mike M. Apr 12 '18 at 21:43
  • @MikeM. My provider is Sprint. I also tried sending myself MMS messages using my default gateway by sending via email @pm.sprint.com. So pretty sure my messaging is MMS. – Kristy Welsh Apr 13 '18 at 14:59

1 Answers1

0

Adding the following to my main activity in the Manifest seems to have solved the problem:

    <activity
        android:name="com.webnation.text2email.main.SMSEmailActivity"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106