As I type this question, I am skeptical there may be no answer?
I am building a flutter app using firebase as the backend. There is a feature in my app that allows a user to add friends to the app from their contact list. Now I am struggling because firebase phone numbers must be stored in this format -- +17653371230. However, when a user stores a phone number in their phone, they don't always add the country code. Essentially, a user can have phone numbers in their contact book in these formats:
9142240145,
510-725-1331,
(404) 988-3125
If I want to check if any of these phone numbers exist in my database, I will get a result saying they don't, even if they could but in a different format. The first step to solving this problem is pretty simple: trim all parentheses and spaces in the phone number string. However it gets difficult and it brings me to my real question:
Give a phone number like so: 9143646532, how can I determine that it is a US number so that I may add the +1? Or given a number like 08027323457, how can I determine that it is a Nigerian number so that I may remove the leading 0 and add a +234?
I want to be able to do this for all countries. Is there a flutter package that handles this? Or is there a publicly known algorithm that sorts this out? Because I doubt I am the first person trying to build this capability using firebase as a backend.
The first step is to normalize these numbers into a format of your choice.
The second step involves finding out the country of residence of the current user. Then determine the country's prefix and add said prefix to the numbers provided by the current user. But check if these numbers already have an international prefix beforehand.
Do you have the complete phone number of the current user with country prefix? Then use that. You could also force the current user to provide or confirm this prefix, if missing.
If not, use a geo-location-service to determine the country of residence of the current user and then select the phone prefix accordingly.
What types of geo-location-service are available to you depends on the framework you use. Some are depending on IPs, some on GPS.
There are also external services:
https://medium.com/mop-developers/free-ip-based-geolocation-with-google-cloud-functions-f92e20d47651
How phone numbers are constructed:
https://www.tipard.com/mobile/international-phone-number-format.html