使用标签时未收到使用天蓝色通知中心的通知

问题描述:

我对此很陌生。机器人的例子是 GetStartedFirebase使用标签时未收到使用天蓝色通知中心的通知

下面是步骤:

1)我安装Android到我的手机

2)我跟着这个例子创建我的网页API 的https //文档.microsoft.com/EN-US /天蓝色/通知,集线器/通知,集线器,ASPNET-后端-GCM-机器人推到用户,谷歌通知

3)我注释掉AuthenticationTestHandler类

4)我调用下面的代码从小提琴

的DeviceRegistration对象

{ 
    "Platform": "gcm", 
    "Handle": "regid i get from android", 
    "Tags": [ 
    "1", 
    "2" 
    ] 
} 

//这创建或在指定的ID

 public async Task<HttpResponseMessage> Put(string id, DeviceRegistration deviceUpdate) 
    { 
     RegistrationDescription registration = null; 
     switch (deviceUpdate.Platform) 
     { 
      case "mpns": 
       registration = new MpnsRegistrationDescription(deviceUpdate.Handle); 
       break; 
      case "wns": 
       registration = new WindowsRegistrationDescription(deviceUpdate.Handle); 
       break; 
      case "apns": 
       registration = new AppleRegistrationDescription(deviceUpdate.Handle); 
       break; 
      case "gcm": 
       registration = new GcmRegistrationDescription(deviceUpdate.Handle); 
       break; 
      default: 
       throw new HttpResponseException(HttpStatusCode.BadRequest); 
     } 

     registration.RegistrationId = id; 

     var username = "test"; 

     string[] userTag = new string[1]; 
     userTag[0] = "username:" + username; 
     registration.Tags = new HashSet<string>(userTag); 
     try 
     { 
      await hub.CreateOrUpdateRegistrationAsync(registration); 
     } 
     catch (MessagingException e) 
     { 
      ReturnGoneIfHubResponseIsGone(e); 
     } 

     return Request.CreateResponse(HttpStatusCode.OK); 
    } 

5更新登记(与设置channelURI) )然后我打电话发送推送通知

http://localhost:4486/api/Notifications?pns=gcm&to_tag=test

public async Task<HttpResponseMessage> Post(string pns, [FromBody]string message, string to_tag) 
{ 

    var user = "test"; 
    message = "msg"; 
    string[] userTag = new string[1]; 
    userTag[0] = "username:" + to_tag;  

    Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null; 
    HttpStatusCode ret = HttpStatusCode.InternalServerError; 

    switch (pns.ToLower()) 
    { 
     case "wns": 
      // Windows 8.1/Windows Phone 8.1 
      var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" + 
         "From " + user + ": " + message + "</text></binding></visual></toast>"; 
      outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag); 
      break; 
     case "apns": 
      // iOS 
      var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}"; 
      outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag); 
      break; 
     case "gcm": 
      // Android 
      var notif = "{ \"data\" : {\"message\":\"" + "From " + user + ": " + message + "\"}}"; 
      outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, userTag); 
      break; 
    } 

    if (outcome != null) 
    { 
     if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) || 
      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown))) 
     { 
      ret = HttpStatusCode.OK; 
     } 
    } 

    return Request.CreateResponse(ret); 
} 

没有错误返回,但我没有收到任何通知。

我尝试如下除去usertag:

outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif); 

我能够收到通知。

为什么标签不起作用?

任何帮助表示赞赏。

+0

你可以去到[诊断指南](https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification -fixer)并用你学到的东西来更新问题? –

我很困惑 - 来自Fiddler的JSON显示注册有“1”和“2”标签,但是在使用“username:test”标签的代码中无处不在。你能从中心获得这个注册并确保它有正确的标签吗? 您可以使用GetRegistrationAsync<TRegistrationDescription>(String) [1],GetRegistrationsByChannelAsync(String, Int32) [2]或GetAllRegistrationsAsync(Int32) [3]方法来获得注册。

[1] https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.notificationhubs.notificationhubclient#Microsoft_Azure_NotificationHubs_NotificationHubClient_GetRegistrationAsync__1_System_String_

[2] https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.notificationhubs.notificationhubclient#Microsoft_Azure_NotificationHubs_NotificationHubClient_GetRegistrationsByChannelAsync_System_String_System_Int32_

[3] https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.notificationhubs.notificationhubclient#Microsoft_Azure_NotificationHubs_NotificationHubClient_GetAllRegistrationsAsync_System_Int32_

+0

最初我确实包含了来自参数的“1”和“2”标记,但它不起作用,所以我只尝试从注册中只使用一个标记并忽略参数中的这些标记,并调用通知api函​​数也只有一个标记来查看如果它有效。遗憾的是仍然无法工作。 – terrance019

+0

之前 Microsoft.Azure.NotificationHubs.NotificationOutcome结果= null; 我确实使用了GetAllRegistrationsAsync(0)来检查,它有一个“username:test”标签 – terrance019

var allRegistrations = await Notifications.Instance.Hub.GetAllRegistrationsAsync(0); 

检查allRegistrations您的标签。如果它在那里,那么它应该工作。

您可以检查测试通知,从http://pushtry.com