Created
October 2, 2019 03:07
-
-
Save ShravanJ/92e5c4254f4e991b01c83bb377cb22bf to your computer and use it in GitHub Desktop.
Get iOS 13 Device Token on Xamarin.iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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