19

I am trying to get EndpointArn by registration id using aws .net sdk. But i couldn't find a nice way to do it.

My first attempt was running CreatePlatformEndpointRequest with the same registrationId, which is registered to SNS Application before, sent by the client(android). With this way, aws api gives you the EndpointArn for this registration id.

Amazon: The CreatePlatformEndpoint action is idempotent, so if the requester already owns an endpoint with the same device token and attributes, that endpoint's ARN is returned without creating a new endpoint.

But if you stored something to CustomUserData before you need to send the same user data to get endpoint arn for this registration id, otherwise you get an exception like below.

Invalid parameter: Token Reason: Endpoint {My Endpoint ARN} already exists with the same Token, but different attributes.

I think i am going to use CreatePlatformEndpoint() without user data and when i need Endpoint Arn for a registrationId i will run CreatePlatFormEndpoint only with this registration id.

Is there another way to get EndpointArn by token better than the solution above ?

Thank you.

Barbaros Alp
  • 6,145
  • 8
  • 44
  • 59
  • Just for info: I just take this decision... And I continue to have the same error. I send "" as user data and I continue to have the error return :s – farvilain May 16 '14 at 22:03
  • 1
    I am facing the same issue. Not sure what is going wrong ? – Saurabh Wadhwa May 20 '14 at 07:43
  • 1
    We have the same problem. see our question: http://stackoverflow.com/questions/22227262/aws-boto-sns-get-endpoint-arn-by-device-token – Amit Talmor May 26 '14 at 10:15
  • 2
    you can get the ARN of end point from the exception message, SNS returns the ARN of already existed end point – iBabur May 15 '15 at 08:31
  • 2
    @babur Seriously that is a terrible solution. What if the format of the error message changes? – LyteSpeed Jun 23 '15 at 00:50
  • @clu that is just a workaround in case someone want to use that. your possible issue also correct "if" in future aws decides to remove arn from exception message – iBabur Jun 23 '15 at 05:51
  • Late reply, but that "workaround" is actually what Amazon suggests in their examples. They have sample code that parses the error message using a regex to extract the ARN. I didn't believe it the first time I saw it. For me it's a very bad practice to do this kind of stuff. – Joris Mans Jun 01 '16 at 07:58
  • By the way CreatePlatformEndpoint request looks idempotent. So you can fire it any times you want. And each time you get same endpointArn in response. But idempotentness require same attributes in request – Deepscorn Nov 09 '17 at 08:45

2 Answers2

10

What we did is store the endpoint ARN returned from CreatePlatformEndpoint in our application database. If we already have an endpoint ARN association for a given device registration id, we then call

getEndpointAttributes passing in the endpoint arn from our database. If a result is returned, we then check if the endpoint is marked enabled and if not then call setEndpointAttributes and set key "Enabled" to "true".

Unfortunately it doesn't look like Amazaon API offers a "findBy" method to look up by "CustomUserData" or "Token" and only has "list" methods that list all the existing endpoints which for most applications will not suffice if there are potentially thousands or millions of entries. It is almost as if API was written only to satisfy the Amazon Console UI use cases.

Also, see Amazon docs for example API requests and responses

http://docs.aws.amazon.com/sns/latest/api/API_GetEndpointAttributes.html http://docs.aws.amazon.com/sns/latest/api/API_SetEndpointAttributes.html

GameSalutes
  • 1,392
  • 2
  • 15
  • 23
8

According to the AWS Blog at https://mobile.awsblog.com/post/Tx223MJB0XKV9RU/Mobile-token-management-with-Amazon-SNS, they also recommend that app side stores the ARN.

retrieve the latest token from the mobile OS
if (endpoint arn not stored)
    # first time registration
    call CreatePlatformEndpoint
    store returned endpoint arn
endif

call GetEndpointAttributes on the endpoint arn 

if (getting attributes encountered NotFound exception)
    #endpoint was deleted 
    call CreatePlatformEndpoint
    store returned endpoint arn
else 
    if (token in endpoint does not match latest) or 
        (GetEndpointAttributes shows endpoint as disabled)
        call SetEndpointAttributes to set the 
                     latest token and enable the endpoint
    endif
endif
Terence
  • 121
  • 2
  • 9