1

I want to create app, that can receive SMS from port! but when run it, my msg.getMessageBody() returns null. How can I fixed it?

manifext.xml

<receiver android:name="data.SMSReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.provider.telephony.SMS_RECEIVED" />
            <data android:scheme="sms"
                android:host="*"
                android:port="4030"/>
        </intent-filter>

Broadcast Receiver code

public class SMSReceiver extends BroadcastReceiver {

private static final String SHORTCODE = "+9810004473";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    Object[] messages = (Object[])bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];
    for(int n=0; n < messages.length; n++) {
        sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }
    for(SmsMessage msg : sms) {
        if(TextUtils.equals(msg.getOriginatingAddress(), SHORTCODE)) {
            Toast.makeText(context, "SMS: " + msg.getMessageBody(), Toast.LENGTH_LONG).show();
        }
    }
}

}

My Toast show: SMS: null

Nikhil
  • 3,661
  • 8
  • 31
  • 43
  • 1
    There won't be a message body on a data SMS message. Use the `SmsMessage#getUserData()` method to retrieve the data, which will be in the form of a `byte` array, as shown in the accepted answer on the linked post above. – Mike M. Sep 27 '16 at 06:52
  • tnx! that's right! – Iman Salehi Sep 27 '16 at 11:05

1 Answers1

0

You can replace this code with your code in onReceive:

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

      Bundle bundle = intent.getExtras(); 

        String recMsgString = "";            
        String fromAddress = "";
        SmsMessage recMsg = null;
        byte[] data = null;
        if (bundle != null)
        {
            //---retrieve the SMS message received---
           Object[] pdus = (Object[]) bundle.get("pdus");
            for (int i=0; i<pdus.length; i++){
                recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);

                try {
                    data = recMsg.getUserData();
                } catch (Exception e){

                }
                if (data!=null){
                    for(int index=0; index<data.length; ++index)
                    {
                           recMsgString += Character.toString((char)data[index]);
                    } 
                }

                fromAddress = recMsg.getOriginatingAddress();
            }

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}    

OK, If you have encoded your message before and you want to decode it right now you must follow blew steps, But if not just delete your entire onReceive and replace my code. You are decrypting your message in a wrong place you must follow the exact steps I wrote. you must get the message here(after getting the data):

    if (data!=null){
                    for(int index=0; index<data.length; ++index)
                    {
                           recMsgString += Character.toString((char)data[index]);
                    } 
                }
String decryptedData = Base64.decode(recMsgString ,...)//add this line

And after getting the message string in recMsgString you can decrypt it using Base64 class.

Milad Faridnia
  • 8,664
  • 13
  • 67
  • 75