Reputation: 639
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.
Upvotes: 6
Views: 4225
Reputation: 4310
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);
}
Upvotes: 2
Reputation: 161
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.
Upvotes: 0
Reputation: 8170
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
}
Upvotes: 5