MonoTouch在iOS8中获取DeviceToken

本文关键字:获取 DeviceToken iOS8 MonoTouch | 更新日期: 2023-09-27 18:22:14

我正在更新我的iOS应用程序以使用iOS8,但在获取远程通知的设备令牌时遇到问题。

我已经更新了我的AppDelegate,以便在使用iOS8时调用RegisterUserNotificationSettings进行注册,使以前的版本可以调用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);
        }

我的AppDelegate类中还有以下方法:

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

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

然而,我不知道如何在DidRegisterUserNotificationSettings 中获得设备令牌

我读过objective-c中有:didRegisterForRemoteNotificationsWithDeviceToken,但这在Xamarin中似乎不可用(或者至少我不知道如何称呼它)。

MonoTouch在iOS8中获取DeviceToken

简单的回答,我在注册时缺少以下代码行:

UIApplication.SharedApplication.RegisterForRemoteNotifications();

添加这一行意味着代码进入了RegisteredForRemoteNotifications处理程序。

因此,注册通知的完整代码是:

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

没有足够的代码来确定哪里出了问题。我怀疑你改变了你的电话(而不仅仅是像这里描述的那样增加了新的电话)。如果这没有帮助(或者不清楚),那么你可能想用更多你正在使用的代码来更新你的问题。

此外,你应该能够取代这个:

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

带有:

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

最后:

我读到objective-c中有:didRegisterForRemoteNotificationsWithDeviceToken但这似乎在Xamarin没有。

你可能指的是application:didRegisterForRemoteNotificationsWithDeviceToken:,在Xamarin.iOS中,它映射到UIApplicationDelegate.RegisteredForRemoteNotifications,你说它不再被调用(在iOS8上)。