Sru*_*kar 11 xamarin.ios xamarin.android azure-notificationhub xamarin.forms deviceid
如何在Xamarin Froms中使用c#在Android和iOS中获取设备唯一ID?我使用Azure Notification Hub发送通知.我指的是这个博客.但在Android中我无法找到相关的"设置"
Joh*_*nes 23
根据你发布的博客http://codeworks.it/blog/?p=260和你的简短问题描述,我试着回答你的问题.
对于Android使用
Android.Provider.Settings.Secure.GetString(Android.App.Application.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
对于iOS,请查看您的blogpost ..或者选择将IdentifierForVendoreg 保存AppDelegate在您的IOSDevice类中并在您的类中返回此值(使用blogpost中的名称).用于UIDevice.CurrentDevice.IdentifierForVendor.ToString()在iOS上获取设备ID.
nzr*_*tmn 10
这里有详细的描述。但实际上您不需要这样做并尝试从每个设备获取 Id。简单地创建一个 Guid 并保存到设备也可以。Xamarin 具有在设备中持久化值的首选项。
您可以创建一个 guid 并将其保存到 Prefecences,如下所示:
var deviceId = Preferences.Get("my_deviceId", string.Empty);
if(string.IsNullOrWhitespace(deviceId))
{
deviceId = System.Guid.NewGuid().ToString();
Preferences.Set("my_deviceId", deviceId);
}
Run Code Online (Sandbox Code Playgroud)
这种方法的好处是您生成的 ID 是当应用程序转移到另一台设备而不是您仍然具有相同 ID 时;但是如果您卸载并重新安装,您将获得一个新的 ID。对于从设备获取 Id 的其他情况,当您将应用程序转移到另一台设备时,它会发生变化。
对于您想从设备获取 Id 的其他情况:
iOS:设备标识符
public string Id => UIDevice.CurrentDevice.IdentifierForVendor.AsString();
Run Code Online (Sandbox Code Playgroud)
Android:串行、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;
}
}
Run Code Online (Sandbox Code Playgroud)
UWP:GetPackageSpecificToken 或 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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10540 次 |
| 最近记录: |