binncheol
binncheol

Reputation: 1453

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).

Upvotes: 5

Views: 2594

Answers (2)

binncheol
binncheol

Reputation: 1453

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();
        }

Upvotes: 5

poupou
poupou

Reputation: 43553

There's not enough code to be sure what's wrong. I suspect you're changed what you're calling (not just added new calls, like described here). If that does not help (or is not clear) then you might want to update your question with more of the code you're using.

Also you should be able to replace this:

NSString str = (NSString)Runtime.GetNSObject(Messaging.intptr_objc_msgSend(deviceToken.Handle, new Selector("description").Handle));
_deviceTokenString = str.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");

with:

_deviceTokenString = deviceToken.Description.Replace("<", "").Replace(">", "").Replace(" ", "");

Finally:

I have read that in objective-c there is: didRegisterForRemoteNotificationsWithDeviceToken but this doesn't seem to be available in Xamarin.

You likely mean application:didRegisterForRemoteNotificationsWithDeviceToken: which, in Xamarin.iOS, maps to UIApplicationDelegate.RegisteredForRemoteNotifications, which you said is not called anymore (on iOS8).

Upvotes: 0

Related Questions