1

This following line sends a message to a particular Whats App contact

"https://api.whatsapp.com/send?phone=" + phone + "&text=" + URLEncoder.encode(msg, "UTF-8");

Reference : How can I send message to specific contact through WhatsApp from my android app?

But the above URL needs phone number with country code. I need to send message to phone number which are not saved with country codes. How to do that any idea?

Usman
  • 1,983
  • 15
  • 28

1 Answers1

0

I mean you definitely need to know the country code somehow. you might be able to get it from the phone (read this answer for instance) in case it is absent. then once you have it you simply write

phone="+"+foundCountryCode+phone;

but before you do that you need to check if the phone nr. doesnt already have a country code as some of the users contacts might be from other countries (we live in a globalized world) so simply check if the phone String begins with "+" or "00" and only perform the adjustment above in case it does not.

I did the following

public static String depuratePhone(String rawPhone){
    String phone=rawPhone.replace(" ","").replace("+","00").replace("-","");
    String depPhone=null;
    if (phone.length()>2){
        if (phone.substring(0,2).contentEquals("00")){
            depPhone=phone;
        }else{
            depPhone="00"+your_current_country_code+phone;
        }
    }
    return depPhone;

}

in order to get your own phones country code amazingly it seems you always have to create your own database of country codes as all you can get from the phone are 2 letter string codes for your country. I am also amazed that there is not a way to get this directly from the phone. Amazing, I wonder what the justification for this is.

the freaking TelephonyManager even lets you get your own phone number if you grant it a ton of permissions but not a simple country code.

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • 1
    Thanks for your reply. We live in a globalised World and that is my exact problem. I can get the device country code but how do I know the country code of a particular phone number if it is saved in phonebook without country code? I might be in living in USA but I can save a phone number of India without +91. The phone string(10 digits) can be saved and often people save without '+' or '00' – Arighna maity Mar 22 '20 at 21:01
  • I think that would rarely happen as it would be impossible to call the person from india while im in the us. i know this first hand actually as i bought my phone in a different country and was too lazy to change the locale – quealegriamasalegre Mar 22 '20 at 23:30
  • Ill add some code from an app I made for my own country, I also had to take account of the fact that plenty of people here have whatsapp accounts from the US or Spain – quealegriamasalegre Mar 22 '20 at 23:32
  • btw there is a dependency you can install to avoid having to create your own DB look at this answer for reference https://stackoverflow.com/a/48873912/10637400 the dependency is this one https://github.com/MichaelRocks/libphonenumber-android – quealegriamasalegre Mar 23 '20 at 04:05