如何从平台通知服务中检索PNS句柄?

时间:2018-09-01 13:47:44

标签: c# azure xamarin.ios azure-notificationhub

我有一个正在连接Azure后端服务的Xamarin.iOS应用程序,并且我希望我的服务将通知发送到客户端应用程序。

Microsoft文档说明了如何针对不同情况设置通知中心。我想我已经掌握了其中的大部分内容,但是我不确定我了解very first part,它是用于平台通知服务中Retrieve PNS Handle的iOS应用程序,如下图所示:

enter image description here

这似乎是一些必须由客户端应用程序单独执行的任务,然后将其传达给后端服务以进行注册。

我有一种感觉,它发生在this section的第10步,当iOS通过方法RegisteredForRemoteNotifications调用应用程序时。在该回调中,为应用程序提供了deviceToken

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);

    Hub.UnregisterAllAsync (deviceToken, (error) => {
        if (error != null)
        {
            System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}", error.ToString());
            return;
        }

        NSSet tags = null; // create tags if you want
        Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => {
            if (errorCallback != null)
                System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
        });
    });
}

问题

我需要发送deviceToken PNS句柄到后端服务来启动注册过程吗?如果没有,我应该如何联系PNS以获得该句柄?

2 个答案:

答案 0 :(得分:1)

方法RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)是告诉委托人该应用程序已成功在“推送通知”服务中注册。

参数“ deviceToken”是一个全局唯一的令牌,用于向推送通知服务标识该设备。

    NSSet tags = null; // create tags if you want

    Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => 
    {
        if (errorCallback != null)
            System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
    });

由于使用的是Azure,因此集线器已将令牌发送到上述方法中的生成远程通知。因此,如果您只想向所有用户推送某些内容,则无需执行其他操作。如果要推送给特定用户,则可以注册标签并将其用作参数。

答案 1 :(得分:0)

信息以documentation的形式出现,但对于C#开发人员而言却不是显而易见的形式。

在Objective-C中,deviceToken由iOS应用提供,如@LucasZ所述,它是在PNS中注册的。

但是我不能立即发送此deviceToken,因为我的服务中使用的AppleRegistrationDescription类不会接受它。

花了一段时间让我对Objective-C更加熟悉,才发现此令牌在发送到Azure之前实际上已经转换:

NSSet* tagsSet = tags?tags:[[NSSet alloc] init];

NSString *deviceTokenString = [[token description]
        stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

    deviceTokenString = [[deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""] uppercaseString];

我已经在C#中实现了类似的东西:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    string pnsHandle = deviceToken.Description
                                  .Replace("<", string.Empty)
                                  .Replace(">", string.Empty)
                                  .Replace(" ", string.Empty)
                                  .ToUpper(); 

    Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);

    Hub.UnregisterAllAsync (pnsHandle, (error) => 
    {
        if (error != null)
        {
            System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}", error.ToString());
            return;
        }

        // In my use case, the tags are assigned by the server based on privileges.
        NSSet tags = null;

        Hub.RegisterNativeAsync(pnsHandle, tags, (errorCallback) => 
        {
            if (errorCallback != null)
                System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
        });
    });
}

要回答我的问题,是的,deviceToken是PNS句柄,但必须对其进行格式化。