Plugie
Plugie

Reputation: 1307

Firebase Auth Get User Country

I am using Firebase auth with google sign in and facebook sign in

Is there any other way to know which country the user is coming from ?

After The app know from which country the user is coming from, then the app will decide which service / future will be shown to the user.

Upvotes: 6

Views: 9845

Answers (2)

JoeGamers
JoeGamers

Reputation: 1

Method 2 is great but changing providers, I use it like this:

await http.get(Uri.parse('https://api64.ipify.org?format=json'));

Upvotes: 0

Vikash Kumar Verma
Vikash Kumar Verma

Reputation: 1078

Method 1: Try this code. This code will return country code based on which country's network it is connected to. see here. It won't work without SIM card.

TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();

Method 2: If your device is connected to internet you can use this link http://ip-api.com/json . It will automatically detect ip address of device return location details based on ip address.

Method 3: You can get location from GPS.

 LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria,true);
Location curLocation = locationManager.getLastKnownLocation(provider);
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
    addresses = geocoder.getFromLocation((double)curLocal.getLatitudeE6(),(double)curLocal.getLongitudeE6(),1);
String countryCode= addresses.get(0).getCountryCode();

Upvotes: 12

Related Questions