通过 Azure 通知中心使用注册 ID 发送推送通知

Send push notifications using Registration ID through Azure Notification Hubs

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

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

它可以工作,但标签数量有限。我正在考虑存储和使用 Hub returns.

的注册 ID

有什么方法可以使用这个 ID 发送通知吗?

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

实际上 NH 仅限制标记的数量每个注册但是每个集线器您可以根据需要进行注册,并且每个注册都可以有唯一的标记,您可以使用它来路由通知。

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

关键字是TAG!如果您为任何注册设备使用任何特定标签,如 Android、IOS、Windows OS 等,您可以向任何特定设备发送通知。

要做到这些,您应该按照以下步骤一一进行;

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

Client Example for 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();

如您所见,我们为选定的 Android 设备使用了唯一标记调用 "UniqueTagForThisDevice"

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

Server Example using Web API to send push selected Android device :

  [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();
  }
  • 最后,您应该使用来自任何辅助平台(Postman、Fiddler 或其他平台)的 "UniqueTagForThisDevice" 标记调用上述 Web API 服务方法。

注意: TAG 不必是 deviceToken 或类似的东西。它只需要特定于每个设备。 但是 我建议你,如果你使用 WebAPI 并且它与 Owin 中间件相关,你可能更喜欢用户名作为唯一标记.我觉得,这个更适合应用场景。通过这种方式,您可以携带从唯一设备向唯一用户发送通知 ;)

就这些了。