Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch Get DeviceToken in iOS8

I am updating my iOS application to work with iOS8 but running into problems getting the device token for remote notifications.

I have updated my AppDelegate to call RegisterUserNotificationSettings to register when using iOS8, leaving previous versions to call RegisterForRemoteNotificationTypes:

var version8 = new Version (8,0);

        if (new Version(UIDevice.CurrentDevice.SystemVersion) < version8) 
        {
            var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
            UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
        }
        else
        {
            var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
        }

I also have the following methods in my AppDelegate class:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        NSString str = (NSString)Runtime.GetNSObject(Messaging.intptr_objc_msgSend(deviceToken.Handle, new Selector("description").Handle));
        _deviceTokenString = str.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
        Trace.trace("Device Token: " + _deviceTokenString);
}

and

public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
    {
        // Get Device Token
    }

However I don't know how to get the device token in DidRegisterUserNotificationSettings

I have read that in objective-c there is: didRegisterForRemoteNotificationsWithDeviceToken but this doesn't seem to be available in Xamarin (or at least I don't know how to call it).

like image 441
binncheol Avatar asked Sep 16 '14 15:09

binncheol


1 Answers

Simple answer, I was missing the following line of code when registering:

UIApplication.SharedApplication.RegisterForRemoteNotifications();

Adding this line meant that the code entered the RegisteredForRemoteNotifications handler.

So the complete code for registering for notifications is:

var version8 = new Version (8,0);
        if (new Version(UIDevice.CurrentDevice.SystemVersion) < version8) 
        {
            var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
            UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
        }
        else
        {
            var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications();
        }
like image 128
binncheol Avatar answered Sep 29 '22 05:09

binncheol