首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MonoTouch Get DeviceToken in iOS8

MonoTouch Get DeviceToken in iOS8
EN

Stack Overflow用户
提问于 2014-09-16 15:54:59
回答 2查看 2.6K关注 0票数 5

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

我已经更新了我的AppDelegate,以便在使用iOS8时调用RegisterUserNotificationSettings来注册,保留以前的版本调用RegisterForRemoteNotificationTypes

代码语言:javascript
复制
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类中还有以下方法:

代码语言:javascript
复制
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);
}

代码语言:javascript
复制
public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
    {
        // Get Device Token
    }

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

我在objective中读到过这样的内容:didRegisterForRemoteNotificationsWithDeviceToken,但这在Xamarin中似乎是不可用的(或者至少我不知道如何称呼它)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-17 08:08:26

简单的回答是,我在注册时遗漏了以下代码行:

代码语言:javascript
复制
UIApplication.SharedApplication.RegisterForRemoteNotifications();

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

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-09-16 18:00:47

没有足够的代码来确定出了什么问题。我怀疑您已经更改了您所调用的内容(而不仅仅是添加了新的调用,比如描述的here)。如果这没有帮助(或者不清楚),那么您可能希望使用更多的代码更新您的问题。

此外,您还应该能够替换以下内容:

代码语言:javascript
复制
NSString str = (NSString)Runtime.GetNSObject(Messaging.intptr_objc_msgSend(deviceToken.Handle, new Selector("description").Handle));
_deviceTokenString = str.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");

通过以下方式:

代码语言:javascript
复制
_deviceTokenString = deviceToken.Description.Replace("<", "").Replace(">", "").Replace(" ", "");

最后:

我在objective中读到过这样的内容:didRegisterForRemoteNotificationsWithDeviceToken,但这在Xamarin中似乎是行不通的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25873325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档