【问题标题】:Azure notification hub device installation and send test message successful but no messages being sent to the appAzure 通知中心设备安装和发送测试消息成功,但没有消息发送到应用程序
【发布时间】:2020-10-29 19:35:46
【问题描述】:

我创建了 Azure 通知中心,并将 Firebase 中的服务器密钥添加到通知中心的“GCM/FCM”部分。之后,我使用共享访问密钥和通知中心名称来创建安装(按照 Azure 文档)。这是我使用的代码:

[FunctionName("TestFunction")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]
        HttpRequest req, ILogger log)
    {
        var deviceUpdate = new DeviceInstallation()
        {
            installationId = "<myInstallationid>",
            pushChannel = "<DeviceTokenGeneratedByFirebase>",
            platform = "fcm",
            tags = new string[] {"notificationhubtag1"},
        };
        
        var responseMessage = new HttpResponseMessage();

        try
        {
            responseMessage = await Put(deviceUpdate);
        }
        catch
        {
            log.LogInformation("exception occured");
        }
        
        return new OkObjectResult(responseMessage);
    }
    
    // Custom API
    public static async Task<HttpResponseMessage> Put(DeviceInstallation deviceUpdate)
    {
        NotificationHubClient hub = new NotificationHubClient(fullAccessConnString, hubName);
        
        Installation installation = new Installation();
        installation.InstallationId = deviceUpdate.installationId;
        installation.PushChannel = deviceUpdate.pushChannel;
        installation.Tags = deviceUpdate.tags;

        switch (deviceUpdate.platform)
        {
            case "mpns":
                installation.Platform = NotificationPlatform.Mpns;
                break;
            case "wns":
                installation.Platform = NotificationPlatform.Wns;
                break;
            case "apns":
                installation.Platform = NotificationPlatform.Apns;
                break;
            case "fcm":
                installation.Platform = NotificationPlatform.Fcm;
                break;
            default:
                throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
        
        await hub.CreateOrUpdateInstallationAsync(installation);
        
        return new HttpResponseMessage(HttpStatusCode.OK);

    }

    public class DeviceInstallation
    {
        public string installationId { get; set; }
        public string platform { get; set; }
        public string pushChannel { get; set; }
        public string[] tags { get; set; }
        
    }

当我运行此代码时,我会收到一条成功消息。 Azure 门户本身并没有显示太多关于 no 的信息。 Azure 通知中心中注册的设备或活动设备信息。但是,我能够通过直接通过此调用向安装 API 发出 GET 请求来确认安装存在:

https://<myNotificationHubNameSpace>.servicebus.windows.net/<myHubName>/installations/<myInstallationId>/?api-version=2015-01

这个调用返回了一个 200 OK 我在前面步骤中创建的安装。响应如下所示:

{"installationId":"<myInstallationId>","pushChannel":"<DeviceTokenGeneratedByFireBase>","pushChannelExpired":false,"platform":"gcm","expirationTime":"9999-12-31T23:59:59.9999999Z","tags":["notificationhubtag1"]}

所以,我去了 Azure 通知中心并从“测试发送”选项卡发送了一条测试消息,并在“发送到”标签字段中使用了“通知中心标签 1”标签。我收到了“通知已成功发送到推送通知系统”的成功消息,并且还获得了注册号。

但是,我没有看到任何通知被发送到应用程序本身。如何调试有关正在推送的此特定消息的更多信息。推到哪里去了?

有没有办法在通知中心本身上找到有关推送消息、已安装设备等的更多信息?我发现一篇关于检查日志的旧帖子说要切换到标准层而不是免费层以获取更多信息。我已切换到标准层,但我看不出在免费层或标准层之间向我显示概览或活动日志的方式有任何差异。

【问题讨论】:

    标签: azure push-notification firebase-cloud-messaging azure-notificationhub


    【解决方案1】:

    通知中心有点像代理。因此,您在执行测试发送时在门户中看到的消息意味着它确实成功地将通知传递给 FCM 以进行发送,并从 FCM 获得成功响应。这也意味着设备令牌是有效的。

    此时通知中心无法提供其他数据,因为它不再在系统中。

    当我们过去看到此类问题时,往往意味着应用配置本身存在一些不正确的地方。处理通知意图时可能缺少一些逻辑。

    从上面的代码 sn-p 来看,您似乎是从某个服务器端而不是从应用程序创建安装。我的假设是这意味着您仍在为通知中心集成 Android SDK,因为正常流程是让设备向中心注册自己。

    我们在此处记录完整的端到端步骤:https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-android-push-notification-google-fcm-get-started

    我主要建议仔细查看 Android 端所需的各种代码更改。清单更新和代码更改。如果您在接收通知时仍然遇到问题,请随时在门户中打开支持请求,我们可以更深入地了解正在发生的事情。

    【讨论】:

    • 我希望客户端应用程序做的唯一一件事就是检索 PNS 句柄(或设备令牌)并将其发送到后端。从那里,后端应该负责通过通知中心将推送通知传递到 Firebase 到用户设备。所以,我假设我已经从客户端应用程序获得了设备令牌并进行了测试。另外,我不确定您提到的正常流程。在 Azure 文档中,它提到了两种注册管理方式 - 从设备或从后端。后者是我想要实现的。
    • 客户端应用程序还需要处理通知的实际显示。由应用程序代码决定如何处理收到的通知。这是一个很常见的用例,“肩膀轻拍”通知显示任何内容。因此,根据提供的信息,我最好的猜测是您注册正确,但客户端应用程序仍需要处理和显示传入的通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2016-05-20
    • 2017-10-04
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多