Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 509
Plugie Avatar asked Dec 16 '16 06:12

Plugie


1 Answers

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();
like image 162
Vikash Kumar Verma Avatar answered Sep 26 '22 02:09

Vikash Kumar Verma