忽略Microsoft.Azure.MessagingNotificationHub构造函数的参数?

时间:2016-10-26 14:33:10

标签: android azure xamarin xamarin.android xamarin.forms

我已经成功实现了一个原型Xamarin表单应用程序,我将其部署到Visual Studio(2015)中的Android移动模拟器中。 我正在使用Google Cloud Messaging作为本机通知服务,该应用程序会将从GCM收到的令牌一次性注册到Azure通知中心。

到目前为止一切运作良好。

然而,在某些时候,我偶然在我的代码中向我的Azure通知中心的名称引入了一个流氓附加字符,该代码构造了客户端NotificationHub对象(使用名为Azure Messaging Component的Xamarin组件)。

构造函数需要3个参数

  • Azure通知中心中定义的通知中心名称
  • 监听连接字符串
  • 作为GCMClient握手的一部分传递给OnRegistered事件的Android上下文对象

删除应用并在模拟器上重新部署 - 通知似乎成功,如果我在第一个构造函数参数中放置了虚假通知集线器名称,设备会收到通知。这个参数只是调试所需的友好字符串吗?我很满意自己不正确的连接字符串会导致构造函数和注册失败。

这是我的OnRegistered方法代码,其意图服务用于处理通知的注册和后续接收。

protected override void OnRegistered(Context context, string registrationId)
{
        Log.Verbose("PushHandlerBroadcastReceiver", "GCM Registered: " + registrationId);
        RegistrationID = registrationId;

        /** "NHub" is the hub name on Azure rather than bogus value of "NHub-"
            In the real code I get this from Constants.HubName
            but hardcoding here to highlight the issue of concern
        **/

        Hub = new NotificationHub("NHub-", Constants.ListenConnectionString, context);   

        try
        {
            Hub.UnregisterAll(registrationId);
        }
        catch (Exception ex)
        {
            Log.Error(MyBroadcastReceiver.TAG, ex.Message);
        }

        var tags = new List<string>() { };
        try
        {
            string templateBodyGCM = "{\"data\":{\"message\":\"$(messageParam)\"}}";
            var hubRegistration2 = Hub.RegisterTemplate(registrationId, "genericMessage", templateBodyGCM, tags.ToArray());
        }
        catch (Exception ex)
        {
            Log.Error(MyBroadcastReceiver.TAG, ex.Message);
        }
 }

任何人都可以确认对NotificationHub对象的API调用是否仅将第一个参数用作调试/跟踪等的不透明字符串,并且其值对于在Azure上设置Notification Hub事务(如注册和收据)并不重要通知?

1 个答案:

答案 0 :(得分:0)

您的答案详情在Notification REST API中。

各种“帮助程序”程序包,例如Xamarin Azure组件,在您的情况下是Mike Smith John Smith Tom Smith Nuget。这反过来又是Android本机Java实现的绑定项目,然后又调用Azure Service Bus REST API。

Azure上的这些端点定义为:

Mike Smith
John Smith
Tom Smith
Jerry Smith
Mike Smith ***
John Smith ***
Tom Smith ***
Jerry Smith ***

因此,您使用的Xamarin.Azure.NotificationHubs.Android名称非常重要,并且必须与Azure控制台中的名称相匹配 /或如果您尝试发送原生GCM / APNS / ...通知,请通过您的应用来自> Azure通知中心。

原始邮件已接收正在通过Google的GCM或Firebase进行,因此https://{namespace}.servicebus.windows.net/{Notification Hub}?api-version=2015-01 名称未被使用,因为在接收邮件时,Azure不在图片中设备,它只是将消息传递到正确的本机消息服务的代理。

仅供参考:是的,首先使用REST API会更有效,尤其是在最终的应用程序大小中。内存消耗和吞吐速度也有所提高,但这取决于发送消息的数量......