MonoTouch Get DeviceToken in iOS8

I am updating an iOS application to work with iOS8, but I am having problems getting a device token for remote notifications.

I updated the AppDelegate app to call RegisterUserNotificationSettings to register when using iOS8, leaving the 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 the 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 do not know how to get the device token in DidRegisterUserNotificationSettings

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

+5
source share
2 answers

The simple answer is, I skipped the following line of code during registration:

 UIApplication.SharedApplication.RegisterForRemoteNotifications(); 

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

Thus, the full registration code for notifications:

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

Not enough code to be sure what happened. I suspect that you have changed what you are calling (not just new calls added, as described here ). If this does not help (or is not clear), you can update your question with more code that you use.

You should also 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(" ", ""); 

from:

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

Finally:

I read that in objective-c there is: didRegisterForRemoteNotificationsWithDeviceToken but this does not seem to be available in Xamarin.

You probably mean application:didRegisterForRemoteNotificationsWithDeviceToken: which in Xamarin.iOS displays UIApplicationDelegate.RegisteredForRemoteNotifications , which you said is no longer called (on iOS8).

0
source

Source: https://habr.com/ru/post/1202693/


All Articles