18

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"

Srusti Thakkar
  • 1,455
  • 2
  • 17
  • 43

2 Answers2

35

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.

Himen Suthar
  • 80
  • 1
  • 2
  • 11
Johannes
  • 868
  • 8
  • 14
  • 4
    For android, Forms.Context is obsolete , use Android.App.Application.Context istead. – Baqer Naqvi Feb 13 '19 at 12:19
  • If you got an error with Android.Provider user this "global::Android.Provider" – cho May 27 '19 at 00:42
  • 1
    I thought it was illegal to store a users device id in ur app i may be wrong on that. – rogue39nin Jun 08 '19 at 15:57
  • 1
    Becareful! 'UIDevice.CurrentDevice.IdentifierForVendor.ToString()' just gives you new guid. When you delete your app an re-install it, The value will change. So it is not exactly device Id. – ayberkzeray Nov 13 '19 at 08:21
9

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 your the Id that you genarated is when the app transfared to another device than you have still same Id; 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;
        }
    }
nzrytmn
  • 3,653
  • 27
  • 23
  • +1 ! And apart from the technical reasons for looking into this method, I am also going the route of app/device guids in secure storage so as to avoid any regulatory issues with pseudonymisation. A physical device id may be considered personal data. – Frank May 07 '21 at 06:45