How can I get Device Unique Id in Android and iOS using c# in Xamarin Froms? I am using Azure Notification Hub for sending Notification. I am referring this blog. But in Android I am not able to find related "Settings"
use UIDevice. CurrentDevice. IdentifierForVendor. ToString() to get the device ID on iOS.
Open your phone's dial pad, usually named “Phone” in the apps drawer. 2. Here dial this code *#*#8255#*#*. As soon as you will enter the last digit, Gtalk Service Monitor will open up and show your Android device ID along with your email.
Xamarin. Essentials provides a single cross-platform API that works with any iOS, Android, or UWP application that can be accessed from shared code no matter how the user interface is created. See the platform & feature support guide for more information on supported operating systems.
According your posted blogpost http://codeworks.it/blog/?p=260 and your short problem description I try to answer your question.
For android use Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
For iOS see your blogpost.. or optionally save the IdentifierForVendor
e.g. in your AppDelegate
and return this value in your IOSDevice
class (using the name in the blogpost). use UIDevice.CurrentDevice.IdentifierForVendor.ToString()
to get the device ID on iOS.
It is detailed described here. But actually you don't need to do like this and trying to get an Id from each devices. Simply creating a Guid and saving to device is also working. Xamarin has Prefences for persitent a Value in a device.
You can create a guid and save it to Prefecences as I did below:
var deviceId = Preferences.Get("my_deviceId", string.Empty);
if(string.IsNullOrWhitespace(deviceId))
{
deviceId = System.Guid.NewGuid().ToString();
Preferences.Set("my_deviceId", deviceId);
}
Benefit of this approach is you will still have same Id after you transferred your app to another device; but if you uninstall and reinstall again than you will get a new Id. For the other cases that you get an Id from device, it will be change when you transfer your app to another device.
For other cases that you want to get Id from device:
iOS: IdentifierForDevice
public string Id => UIDevice.CurrentDevice.IdentifierForVendor.AsString();
Android: Serial, getSerial & AndroidId
string id = string.Empty;
public string Id
{
get
{
if (!string.IsNullOrWhiteSpace(id))
return id;
id = Android.OS.Build.Serial;
if (string.IsNullOrWhiteSpace(id) || id == Build.Unknown || id == "0")
{
try
{
var context = Android.App.Application.Context;
id = Secure.GetString(context.ContentResolver, Secure.AndroidId);
}
catch (Exception ex)
{
Android.Util.Log.Warn("DeviceInfo", "Unable to get id: " + ex.ToString());
}
}
return id;
}
}
UWP: GetPackageSpecificToken or GetSystemIdForPublisher
string id = null;
public string Id
{
get
{
if (id != null)
return id;
try
{
if (ApiInformation.IsTypePresent("Windows.System.Profile.SystemIdentification"))
{
var systemId = SystemIdentification.GetSystemIdForPublisher();
// Make sure this device can generate the IDs
if (systemId.Source != SystemIdentificationSource.None)
{
// The Id property has a buffer with the unique ID
var hardwareId = systemId.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
id = Convert.ToBase64String(bytes);
}
}
if (id == null && ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification"))
{
var token = HardwareIdentification.GetPackageSpecificToken(null);
var hardwareId = token.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
id = Convert.ToBase64String(bytes);
}
if (id == null)
{
id = "unsupported";
}
}
catch (Exception)
{
id = "unsupported";
}
return id;
}
}
There is a plugin presenting by James Montemagno.
Search it in NuGet Packages as Xam.Plugin.DeviceInf. Github link: https://github.com/jamesmontemagno/DeviceInfoPlugin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With