如何获取 iOS 台设备的 Azure 通知中心 RegistrationId?

How do I obtain the Azure Notification Hub RegistrationId for iOS devices?

我正在将 Azure 通知中心实施到跨平台 Xamarin 应用程序中。我唯一标识注册通知的每个设备的方法是临时存储 azure 分配给 phone 的 RegistrationId。用户登录后,我会将 RegstrationId 和用户的一些独特特征发送到后端服务。 RegistrationId 用于识别 phone,然后将用户的详细信息添加到 phone 作为标记通知的标签。

这适用于 Android,因为我需要做的就是获取 registrationId 如下:

var regID = hub.Register(token, tags.ToArray()).RegistrationId;

但是 iOS 的等效项不起作用,因为 class SBNotificationHub 不包含任何获取 RegistrationId 的方法。如何为 iOS 台设备获取此 Azure RegistrationId?

谢谢。

我相信您可以在 AppDelegate class 中的 RegisteredForRemoteNotifications 覆盖中获得设备 ID,如 this documentation 中所述。

尝试将以下内容添加到您的 AppDelegate class:

// Add to top of class file, outside of namespace
using Newtonsoft.Json.Linq;

// Add inside of class
public override void RegisteredForRemoteNotifications(UIApplication application,
NSData deviceToken)
{
    const string templateBodyAPNS = "{\"aps\":{\"alert\":\"$(messageParam)\"}}";

    JObject templates = new JObject();
    templates["genericMessage"] = new JObject
    {
        {"body", templateBodyAPNS}
    };

    // Register for push with your mobile app
    Push push = TodoItemManager.DefaultManager.CurrentClient.GetPush();
    push.RegisterAsync(deviceToken, templates);
}

我认为 deviceToken 就是您在 iOS 中寻找的东西?