2013-10-23 59 views
8

我正在使用亞馬遜AWS Ruby SDK用於Amazon SNS,但我在註冊設備時遇到了一些問題。有時,當設備被重新註冊我得到這樣AWS::SNS::Errors::InvalidParameter Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-east-1:**** already exists with the same Token, but different attributes.錯誤。如何檢查端點是否已經存在,更重要的是,我如何得到一個給定的令牌端點?如何檢查移動設備是否已註冊

+0

我與SNS的node.js有同樣的問題。你有沒有找到一些解決方案? –

+0

我正在使用.net庫,並且在嘗試重新註冊時沒有收到錯誤 –

+0

只有當您希望存儲與註冊相關的附加屬性時纔會出現錯誤,然後稍後您將使用不同的設置的屬性。 –

回答

10

感謝BvdBijl的想法,我做了一個擴展方法來刪除發現現有的一個,如果再添​​加它。

using System; 
using System.Text.RegularExpressions; 
using Amazon.SimpleNotificationService; 
using Amazon.SimpleNotificationService.Model; 

namespace Amazon.SimpleNotificationService 
{ 
    public static class AmazonSimpleNotificationServiceClientExtensions 
    { 
     private const string existingEndpointRegexString = "Reason: Endpoint (.+) already exists with the same Token"; 
     private static Regex existingEndpointRegex = new Regex(existingEndpointRegexString); 
     public static CreatePlatformEndpointResponse CreatePlatformEndpointIdempotent(
      this AmazonSimpleNotificationServiceClient client, 
      CreatePlatformEndpointRequest request) 
     { 
      try 
      { 
       var result = client.CreatePlatformEndpoint(request); 
       return result; 
      } 
      catch (AmazonSimpleNotificationServiceException e) 
      { 
       if (e.ErrorCode == "InvalidParameter") 
       { 
        var match = existingEndpointRegex.Match(e.Message); 
        if (match.Success) { 
         string arn = match.Groups[1].Value; 
         client.DeleteEndpoint(new DeleteEndpointRequest 
         { 
          EndpointArn = arn, 
         }); 
         return client.CreatePlatformEndpoint(request); 
        } 
       } 
       throw; 
      } 
     } 
    } 
} 
+3

這麼瘋狂,這實際上是我可以看到的唯一方法它也是如此。 – chadkouse

+2

不用刪除和創建平臺終端,你可以只調用: SetEndpointAttributes 與屬性 '啓用= TRUE; – Kamil

+0

感覺奇怪不必解析了這一點,可能會改變未來的錯誤消息。很荒謬,他們不只是返回重複的端點阿爾恩給我們。 – user3344977

0

它看起來像amazone解決了這個問題。 我使用回報率和嘗試註冊和現有GCM代碼時曾經有過同樣的問題,我得到了一個錯誤信息說

"AWS::SNS::Errors::InvalidParameter Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-east-1:**** already exists with the same Token, but different attributes." 

雖然我用相同(空)的屬性。現在,當我把現有GCM代碼(有相同的屬性與原)我得到的端點阿爾恩,而不是錯誤消息。

0

ListEndpointsByPlatformApplication僅返回100個端點,您必須使用nextToken才能獲得更多。這是我的實現。

public void deleteEndpoint(string token, string PlatformApplicationArn) 
    { 
     ListEndpointsByPlatformApplicationRequest listRequest = new ListEndpointsByPlatformApplicationRequest(); 
     listRequest.PlatformApplicationArn = PlatformApplicationArn; 
     Logger.Info("Deleting endpoint with token -> " + token); 
     var list = snsClient.ListEndpointsByPlatformApplication(listRequest); 
     do 
     { 
      foreach (var x in list.Endpoints.Where(x => x.Attributes["Token"] == token)) 
      { 
       snsClient.DeleteEndpoint(new DeleteEndpointRequest() { EndpointArn = x.EndpointArn }); 
       Logger.Info("Endpoint removed-> " + x.EndpointArn); 
       return; 
      } 

      listRequest.NextToken = list.NextToken; 
      list = snsClient.ListEndpointsByPlatformApplication(listRequest); 
     } 
     while (list.NextToken != null); 

    } 
+0

考慮一下,如果我有足夠的終點是指1個十萬,那麼這個過程將需要一些什麼權利推遲檢查總終點的令牌。有沒有辦法直接刪除端點而不進行處理? –