2

我正在尝试使用 Azure 通知中心向客户端发送推送通知。我读了这篇文章,它使用标签来识别每个用户。

https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-aspnet-backend-windows-dotnet-notify-users/

它可以工作,但标签的数量是有限的。我正在考虑存储和使用集线器返回的注册 ID。

有没有办法使用这个 ID 发送通知?

另一种方法是使用 WNS 返回的 Channel.URI。这可以以某种方式实现吗?

4

2 回答 2

2

实际上,NH 仅限制每个注册的标签数量,但每个集线器您可以拥有任意数量的注册,并且每个注册可能有唯一的标签,您可以使用它来路由通知。

还有新的通知中心安装 API,我相信它更适合你。它仍然没有很好的文档记录,但做得很好并且可以使用。在这里,您可以找到有关如何使用该 API 的简短说明。自述文件是关于 Java 的,但 .NET SDK 具有几乎相同的功能(最终都调用相同的 REST API)。

于 2015-10-16T19:05:35.983 回答
1

关键字是标签!如果您对任何已注册的设备(Android、IOS、Windows 操作系统等)使用任何特定标签,您可以向任何特定设备发送通知。

为此,您应该一一按照以下步骤操作;

  • 作为客户端,使用特定标记将设备注册到选定的 Azure 通知中心

Android 客户端示例:

`/*you don't have to use Firebase infrastructure. 
  You may use other ways. It doesn't matter.*/`
   String FCM_token = FirebaseInstanceId.getInstance().getToken();
   NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
                                  NotificationSettings.HubListenConnectionString, context);
   String registrationID = hub.register(FCM_token, "UniqueTagForThisDevice").getRegistrationId();

如您所见,我们"UniqueTagForThisDevice"为选定的 Android 设备使用了独特的标签调用。

  • 作为服务器端,您应该使用该 TAG call 发送通知 "UniqueTagForThisDevice"

服务器示例使用 Web API 发送推送选定的 Android 设备:

  [HttpGet]
  [Route("api/sendnotification/{deviceTag}")]
  public async Task<IHttpActionResult> sendNotification(string deviceTag)
  {
      //deviceTag must be "UniqueTagForThisDevice" !!!
      NotificationHubClient Hub = NotificationHubClient.CreateClientFromConnectionString("<DefaultFullSharedAccessSignature>");
      var notif = "{ \"data\" : {\"message\":\"Hello Push\"}}";
      NotificationOutcome outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif,deviceTag);
      if (outcome != null)
      {
         if (!((outcome.State == NotificationOutcomeState.Abandoned) ||
            (outcome.State == NotificationOutcomeState.Unknown)))
            {
                return Ok("Push sent successfully.");
            }
      }
      //Push sending is failed.
      return InternalServerError();
  }
  • "UniqueTagForThisDevice"最后,您应该使用来自任何帮助平台(Postman、Fiddler 或其他平台)的标记调用上述 Web API 服务方法。

注意: TAG不必是 deviceToken 或类似的东西。它只需要针对每个设备。我建议你,如果你使用 WebAPI 并且它与 Owin midlleware 相关,你可能更喜欢将用户名作为唯一标签。我认为,这更适用于应用场景。这样,您可以携带从唯一设备向唯一用户发送通知;)

就这样。

于 2016-12-27T08:27:02.137 回答