Search code examples
xamarin.iosapple-push-notificationsapns-sharp

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.


Solution

  • 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
    }