Skip to content

Instantly share code, notes, and snippets.

@ShravanJ
Created October 2, 2019 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShravanJ/92e5c4254f4e991b01c83bb377cb22bf to your computer and use it in GitHub Desktop.
Save ShravanJ/92e5c4254f4e991b01c83bb377cb22bf to your computer and use it in GitHub Desktop.
Get iOS 13 Device Token on Xamarin.iOS
public override async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
var token = "";
/* Tokens generated on devices running iOS 13+ follow a different format which requires conversion for the backend to process them
More info: https://nshipster.com/apns-device-tokens/
*/
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
byte[] result = new byte[deviceToken.Length];
Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
token = BitConverter.ToString(result).Replace("-", "");
}
else
{
token = deviceToken.Description;
token = DeviceToken.Trim('<').Trim('>');
token = DeviceToken.Replace(" ", "");
}
if (!string.IsNullOrWhiteSpace(token))
{
System.Diagnostics.Debug.WriteLine("iOS Device Token: " + token);
// Save the token using your prefered method (Xamarin.Essentials's Preferences works well)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment