【问题标题】:Azure notification hub installation c#Azure 通知中心安装 c#
【发布时间】:2018-04-01 16:47:29
【问题描述】:

谁能帮助我使用 Azure 通知中心,如何设置设备安装表单 c# 代码。我对 Installation 对象有疑问。如何将其设置为将其作为参数传递给中心客户端实例的 CreateOrUpdateInstallation 方法。我不清楚。

我在 azure 上有一个集线器,可以在本地进行设备注册,如魅力,但上传到 azure 上不起作用。现在我想尝试一下。

thnx

更新:4 天后,我发现您无法向自己发送通知。 Azure 不知何故知道您正在向您的手机发送通知,这就是为什么我的欢迎消息从未发送到我的手机。

更新:这就是我现在在后端代码中安装设备的方式:

[HttpGet]
[Route("api/push/test-installation")]
public async Task<IActionResult> NotificationInstalationTest()
{
 string connectionString = "{{my connection string}}";
 string hubName = "{{my hub name}}";
 string token = "{{tokne}}";

 NotificationHubClient hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);

 string notificationText = $"Test message for Azure delivery for Atila at: {DateTime.Now.ToShortTimeString()}";

 var alert = new JObject
 (
      new JProperty("aps", new JObject(new JProperty("alert", notificationText))),
      new JProperty("inAppMessage", notificationText)
 ).ToString(Newtonsoft.Json.Formatting.None);

 IList<string> tags = new List<string>();
 tags.Add("email");

 IDictionary<string, string> pushVariables = new Dictionary<string, string>();
 pushVariables.Add( "email", "atila@panonicit.com" );

 Installation installation = new Installation();
 installation.InstallationId = Guid.NewGuid().ToString();
 installation.Platform = NotificationPlatform.Apns;
 installation.PushChannel = token;
 installation.Tags = tags;
 installation.PushVariables = pushVariables;

 await hubClient.CreateOrUpdateInstallationAsync(installation);
 NotificationOutcome result = await hubClient.SendAppleNativeNotificationAsync(alert);

 return Ok("Success");
}

现在,当我使用 Postman 访问此端点时,它可以工作,如果来自 iOS 的相同端点调用它不起作用!

thnx

【问题讨论】:

    标签: ios azure-notificationhub


    【解决方案1】:

    如何设置它以将其作为参数传递给中心客户端实例的 CreateOrUpdateInstallation 方法。我不清楚。

    据我了解,您正在使用安装模型从后端注册通知中心。对于您的 WebAPI 项目,假设您创建/更新安装的方法如下:

    InstallationController.cs

    //PUT api/installation
    public async Task<HttpResponseMessage> Put(DeviceInstallation deviceUpdate)
    {
        Installation installation = new Installation();
        installation.InstallationId = deviceUpdate.InstallationId;
        //TODO:
        await hub.CreateOrUpdateInstallationAsync(installation);
        return Request.CreateResponse(HttpStatusCode.OK);
    }
    

    对于您的移动客户端,您可以参考以下方法:

    private async Task<HttpStatusCode> CreateOrUpdateInstallationAsync(DeviceInstallation deviceInstallation)
    {
        using (var httpClient = new HttpClient())
        {
            //TODO: set your authorization header
            //httpClient.DefaultRequestHeaders.Authorization
    
            var putUri =$"{your-backend-endpoint}/api/installation";
            string json = JsonConvert.SerializeObject(deviceInstallation);
            var response = await httpClient.PutAsync(putUri, new StringContent(json, Encoding.UTF8, "application/json"));
            return response.StatusCode;
        }
    }
    

    另外,更多细节你可以参考Registration management和here用注册模式搭建后端,用安装模式搭建你的后端。

    【讨论】:

    • 谢谢答案。是的,我正在尝试通过后端安装设备,因为注册不起作用,当我上传到 Azure 时它简单地不传递消息。只有当我用 Postman 打控制器时,设备安装才能在 Azure 上运行。有什么想法吗?
    • 从您的 IOS 调用端点时有任何异常吗?您可以尝试关注here 来解决此问题。
    • 没有例外,没有。那就是问题所在。就像一个黑匣子,我们看不到为什么设备来电时不推送。
    • 您是否尝试通过 azure 门户上的测试发送刀片发送通知?此外,请尝试在“监控 > 指标”下查看通知中心的指标,了解 APNS 相关指标,以缩小此问题的范围。
    • 是的,我试过了,它成功了。它也从 VS2015 提供。只有当用户使用手机登录时才收到。为了测试,我们在第一次登录时添加了一条欢迎消息,当设备注册时,如果我用相应型号的邮递员点击控制器,它可以工作,如果模型来自手机它不工作。这是我用大量代码 sn-ps 发布我的问题的地方:stackoverflow.com/questions/46778371/…
    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2020-05-22
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多