【问题标题】:Argument to Microsoft.Azure.MessagingNotificationHub constructor being ignored?忽略 Microsoft.Azure.MessagingNotificationHub 构造函数的参数?
【发布时间】:2017-03-08 23:40:03
【问题描述】:

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

到目前为止一切正常。

但是,在某些时候,我在构建客户端 NotificationHub 对象(使用称为 Azure 消息传递组件的 Xamarin 组件)的代码中不小心在我的 Azure 通知中心的名称中引入了一个流氓附加字符。

构造函数接受 3 个参数

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

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

这是我的 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 事务(例如注册和接收)并不重要通知?

【问题讨论】:

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


    【解决方案1】:

    您的答案的详细信息在通知 REST API 中。

    各种“帮助程序”包,例如 Xamarin Azure 组件,在您的情况下是 Xamarin.Azure.NotificationHubs.Android Nuget。这又是一个绑定项目,用于 Android 本机 Java 实现,然后它又调用 Azure 服务总线 REST API。

    Azure 上的那些端点定义为:

    https://{namespace}.servicebus.windows.net/{Notification Hub}?api-version=2015-01
    

    因此,您使用的 Notification Hub 名称很重要,并且必须与您的 Azure 控制台中的名称匹配如果您正在从应用程序中创建、查询、删除 Hub AND/OR 如果您尝试通过 Azure 通知中心您的应用发送本机 GCM/APNS/... 通知。

    开始收到的本机消息正在通过 Google 的 GCM 或 Firebase,因此Notification Hub 名称没有被使用,因为在设备上接收消息时 Azure 不在图片中,它只是将消息发送到正确的本机消息服务的代理。

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

    【讨论】:

    • 非常有用的答案,尤其是您对直接使用 RESTful API 可能获得的效率的评论。在我的情况下,移动应用程序正在注册应用程序首次启动时从 GCM 返回的令牌(registrationid)(或者,在应用程序从设备中删除并重新安装的情况下)。我想我可以嗅探 RESTful 调用以查看发生了什么,但在我看来,NotificationHub 构造的对象不需要此注册步骤的准确集线器名称。
    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2021-04-29
    • 2020-11-07
    • 2020-04-11
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    相关资源
    最近更新 更多