gpt4 book ai didi

c# - 如何通过 Web 应用程序向 Azure 通知中心注册远程设备

转载 作者:行者123 更新时间:2023-12-03 05:04:17 28 4
gpt4 key购买 nike

一些背景...我正在构建一个 Xamarin.Forms 移动应用程序,以允许监控用户家中设备的能源数据(该数据被推送到后端并每分钟存储一次)。我希望能够从我的(Azure 辅助角色)后端通过 Azure 通知中心发送推送通知。有时我想推送到所有设备,有时推送到组,有时推送到单个设备(我相信“标签”是做到这一点的方法)。我当前配置了 Azure 通知中心,可以将测试消息(从 Azure 门户)发送到我用于测试的所有 iOS 设备。

我遇到的麻烦是弄清楚如何向通知中心注册每个设备。我想要发生的是,当用户登录移动应用程序时,设备“句柄”(或设备 token )被传回服务器。然后可以将其保存在后端,并用于使用基于 https://msdn.microsoft.com/en-us/library/azure/dn743807.aspx 中提供的示例的代码来注册设备。和 Registering device on Azure Notification Hub from ASP.NET ,以及对后端相关的“标签”进行更改。如果我需要向单个设备或设备组发送通知,我可以从辅助角色中获取设备句柄。

我发现了许多示例,但这些示例似乎都缺少使我能够正常工作的重要信息。我发现的大多数示例似乎都依赖于 Azure 移动应用程序(和 MobileServiceClient 类)的使用,但我使用的是“常规”MVC5 ASP.NET Web 应用程序。我注意到,对于移动应用程序和 Web 应用程序,Azure 门户允许我配置“推送”,因此想必我可以支持来自任一应用程序的推送。我尝试将 RegisterController 添加到我的 Web 应用程序中,但这在调用时似乎没有帮助:

var push = PushNotificationsManager.DefaultManager.CurrentClient.GetPush();
push.RegisterAsync(deviceToken, templates);

基于以下位置提供的代码示例:https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push#create-hub 。推送客户端所在位置:

client = new MobileServiceClient("http://mywebapp.azurewebsites.net");

RegisterAsync 尝试调用哪个端点?我没有收到任何错误。

Registering device on Azure Notification Hub from ASP.NET似乎建议我需要将 RegisterController 添加到我的 Web 应用程序,但它没有显示调用它的 Xamarin.Forms 代码。

那么,如何通过 MVC5 Web 应用程序从 Xamarin.Forms 应用程序向 Azure 通知中心注册移动设备?

最佳答案

你正在寻找的是这样的东西。

在您的 Web api 中获取用户 Ios 设备的 token 。

   var _hubClient=NotificationHubClient.CreateClientFromConnectionString(YourConnectionstring here)
//this is notification hub client
var registrationId= await _hubClient.CreateRegistrationIdAsync();

上面的代码将为您创建一个registrationId。之后

   var registration = new GcmRegistrationDescription(devicetoken)
{
RegistrationId = RegistrationId , //one we got in previous
call.
Tags = new HashSet<string>
{
"YourDesiredTag"
}
};

//this completes registration of your device with desired tags.
_hubClient.CreateOrUpdateRegistrationAsync(registration);

现在您的注册已完成。您可以简单地从您的工作角色发送针对标签的通知,并且通知应该到达所需的设备。

 _hubClient.SendGcmNativeNotificationAsync(notifPayload, tags)

您应该在此处获取更多信息 https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-registration-management

上面的代码可能对 gcm/android 有效,但是是可以遵循的标准。

关于c# - 如何通过 Web 应用程序向 Azure 通知中心注册远程设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48530943/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com