我正在更新我的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中读到过这样的内容:didRegisterForRemoteNotificationsWithDeviceToken,但这在Xamarin中似乎是不可用的(或者至少我不知道如何称呼它)。
发布于 2014-09-17 08:08:26
简单的回答是,我在注册时遗漏了以下代码行:
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();
}发布于 2014-09-16 18:00:47
没有足够的代码来确定出了什么问题。我怀疑您已经更改了您所调用的内容(而不仅仅是添加了新的调用,比如描述的here)。如果这没有帮助(或者不清楚),那么您可能希望使用更多的代码更新您的问题。
此外,您还应该能够替换以下内容:
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中读到过这样的内容:
didRegisterForRemoteNotificationsWithDeviceToken,但这在Xamarin中似乎是行不通的。
您可能指的是application:didRegisterForRemoteNotificationsWithDeviceToken:,在Xamarin.iOS中,它映射到UIApplicationDelegate.RegisteredForRemoteNotifications,您说它不再被称为(在iOS8上)。
https://stackoverflow.com/questions/25873325
复制相似问题