Search code examples
javaandroidhyperlinksmssmsmanager

Text message not getting sent


I am trying to send the message to multiple numbers. But the message is not getting sent. I have added the code to send the messages in a service to make sure the code gets executed even if the app is in the background.

Also I added the permission in the manifest and asked for runtime permission as well.

Message Service:

public class MessageService extends Service {

    ArrayList<String>  numbers = new ArrayList<>();
    private SharedPreferences sharedpreferences;
    private String mUserName;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        sharedpreferences = getSharedPreferences("UserProfile", Context.MODE_PRIVATE);

        mUserName = sharedpreferences.getString("UserUsername", "");

        numbers = intent.getStringArrayListExtra("numbers");

        for (String number:numbers) {

              sendMsg(number);

           // sendSMS("8655864341","Hello");
        }

        return super.onStartCommand(intent, flags, startId);
    }

    public void sendMsg(final String num){
        String SENT = "SMS_SENT";
        final SmsManager sms = SmsManager.getDefault();
        final PendingIntent sentPI = PendingIntent.getBroadcast(MessageService.this, 0,new Intent(SENT), 0);
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
              //  sms.sendTextMessage(num, null, "Hi,I am " + mUserName + "add me to your unique contact list and you never need to update" +
                      //  " any changes anymore! Click download below to download the App." + "https://play.google.com/apps/testing/com.weberz", sentPI, null);

                sms.sendTextMessage(num, null, "Hi,add me to your unique contact list and you never need to update" +
                        " any changes anymore! Click download below to download the App.", sentPI, null);
            }
        }, 3000);


    }

}

This thing was working before, messages were being received on the numbers. Now I tried to add the link and username in the message and they were not getting sent.

Then again I tried to remove link and username from the messages and checked if the messages were getting sent and it's not working now.

I also wanted to know how to add hyperlinks in SMS so that a user can open a web page after clicking the link?


Solution

  • If the SMS messages were getting received in the past, but after attempting to send a hyperlink it doesn't work, it could be that the wireless provider you're using have blocked your SIM card for suspicious SMS traffic. Messages with links are often treated as spam.

    Try to replace the SIM card and send a "Hello World" message once again.

    The hyperlinks in SMS are recognized by the phone and the most smart phones make them clickable. So, just put the URL in SMS and recipients can click it. It works on iPhone and the most of Android phones.