Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deviceToken of IOS Device

I am using Monotouch for mac and have gone through the steps to retrieve a provisioning profile certificate enabling push notification in the process. I have a working app and am now experimenting with apns-sharp and moon-apns but cant' figure out how to retrieve my device token. I'm hoping someone can provide me with detailed and straightforward steps to achieve this.

like image 287
John D Avatar asked Mar 13 '12 09:03

John D


People also ask

How do I find my iOS device token?

Install either Xcode or iPhone Configuration UtilityHighlight your device name on the left, and go to the Console tab. Copy the above, remove spaces if present. This is your device token.

Do device tokens change iOS?

The main problem with device tokens is that they can change and become an unreliable method for addressing notification delivery causing issues with: missing notifications. incorrect delivery. duplicate notifications.

What is APNs device token?

The push notification networks identify each device by device token. A device token is not a device IMEI but an ID to identify a certain mobile app on a certain device. It is issued by calling libraries of FCM, JPush, or APNs.


2 Answers

In your FinishedLaunching method, register the app for remote notifications, through the UIApplication object you get in it:

// Pass the UIRemoteNotificationType combinations you want
app.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Alert |
 UIRemoteNotificationType.Sound);

Then, in your AppDelegate class, override the RegisteredForRemoteNotifications method:

public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
    // The device token
    byte[] token = deviceToken.ToArray();
}

You also have to override the FailedToRegisterForRemoteNotifications method, to handle the error, if any:

public override void FailedToRegisterForRemoteNotifications (UIApplication application, NSError error)
{
    // Do something with the error
}
like image 51
Dimitris Tavlikos Avatar answered Sep 29 '22 07:09

Dimitris Tavlikos


As of iOS deviceToken has changed. The following code worked for me to convert deviceToken as NSData to a string.

string deviceTokenString;
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
    deviceTokenString = BitConverter.ToString(deviceToken.ToArray()).Replace("-", string.Empty);
}
else
{
    deviceTokenString = Regex.Replace(deviceToken.ToString(), "[^0-9a-zA-Z]+", string.Empty);
}
like image 30
ProfNimrod Avatar answered Sep 29 '22 05:09

ProfNimrod