【问题标题】:How to retrieve a PNS Handle from the Platform Notification Service?如何从平台通知服务中检索 PNS 句柄?
【发布时间】:2019-02-07 06:26:59
【问题描述】:

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

Microsoft 文档解释了如何为不同的场景设置通知中心。我想我已经掌握了大部分内容,但是我不确定我是否理解了very first part,它是用于iOS 应用程序从平台通知服务到Retrieve PNS Handle,如下图所示:

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

我有一种感觉,它发生在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 获取 Handle?

【问题讨论】:

  • 我的回答有用吗?
  • @LucasZ 我没试过。明天做???

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


【解决方案1】:

该信息在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 句柄,但必须格式化。

【讨论】:

    【解决方案2】:

    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,Hub 已将令牌发送到上述方法中的生成远程通知。因此,如果您只想将某些内容推送给所有用户,则无需执行其他操作。如果要推送给特定用户,可以注册一个标签作为参数使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多