deviceToken of IOS Device
Asked Answered
J

3

6

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.

Jutland answered 13/3, 2012 at 9:6 Comment(0)
V
5

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
}
Vouch answered 13/3, 2012 at 9:58 Comment(2)
do I have to go through all these steps?...Isnt there a way to retrieve the actual hex value of the device id?Jutland
No, there isn't another way. Your app has to register for remote notifications every time it starts. If you are referring to the UDID, retrieving it on iOS 5+ will get your app rejected. Furthermore, the device token for remote notifications is different than the UDID and is based on your provisioning profile, notification types etc.Vouch
R
2

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);
}
Rowell answered 26/11, 2019 at 21:44 Comment(1)
This saved me a lot of headache. Thanks!Miscegenation
I
0

For me this was only half of the resolution. To use the DeviceToken from a webserver (PHP in my case), the DeviceToken needs to be a Hex String used in the PHP code for firing the Push Notification (e.g. as shown here: [Using PHP to send iOS Push Notifications via APNs

However, the NSdata object offers no simple way to provide that Hex String.

So my "RegisteredForRemoteNotifications" success handler is now:

        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        // Get current device token
        var DeviceToken = Tools.ByteToHex(deviceToken.ToArray());
        string DeviceID = UIDevice.CurrentDevice.IdentifierForVendor.AsString();
        System.Console.WriteLine("### UserNotification Device Token = " + DeviceToken + ", DeviceID = " + DeviceID);

        // Get previous device token
        var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

        // Has the token changed?
        if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
        {
            //### todo: Populate POSTdata set
            //### todo: Send POSTdata to URL 

            // Save new device token
NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
            }
        }

And for the Byte to Hex Conversion:

public static string ByteToHex(byte[] data)
{
    StringBuilder sb = new StringBuilder(data.Length * 2);
    foreach (byte b in data)
    {
        sb.AppendFormat("{0:x2}", b);
    }
    return sb.ToString();
}

Now you can use the DeviceToken in PHP to create the PushNotification submission.

Inkstand answered 22/10, 2019 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.