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

时间:2015-10-16 12:28:11

标签: windows push-notification windows-phone-8.1 azure-notificationhub wns

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

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

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

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

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

2 个答案:

答案 0 :(得分:2)

实际上,NH仅限制每次注册的标签数量,但每个中心您可以根据需要进行多次注册,并且每个注册可能都有唯一标记,您可以使用该标记来路由通知。

此外,还有针对通知中心的新安装API,我相信这对您更有帮助。它仍然没有很好的文件记录,但做得很好,随时可以使用。 Here您可以找到有关如何使用该API的简短说明。自述文件是关于Java的,但.NET SDK具有几乎相同的功能(最终都调用相同的REST API)。

答案 1 :(得分:2)

关键字 TAG !如果您对任何注册设备(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();

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

  • 作为服务器端,您应使用该TAG调用"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();
  }
  • 最后,您应该使用来自任何帮助平台(Postman,Fiddler或其他人)的"UniqueTagForThisDevice"标记来调用上面的Web API服务方法。

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

这就是全部。