2017-02-10 30 views
2

我對此很陌生。機器人的例子是 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)並用你學到的東西來更新問題? –

回答

0

我很困惑 - 來自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

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

檢查allRegistrations您的標籤。如果它在那裏,那麼它應該工作。

您可以檢查測試通知,從http://pushtry.com