Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check whether a mobile device has already been registered

I'm using the Amazon AWS Ruby SDK for Amazon SNS but I'm having some trouble with devices already being registered. Sometimes when a device gets registered again I get an error like AWS::SNS::Errors::InvalidParameter Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-east-1:**** already exists with the same Token, but different attributes.. How do I check whether an endpoint already exists and more importantly, how do I get the endpoint for a given token?

like image 599
BvdBijl Avatar asked Oct 23 '13 19:10

BvdBijl


People also ask

Is someone connected to my phone?

Signs That Someone Has Remote Access to Your PhoneThe battery drains quickly even when not in use. Higher data usage than usual. Noises in the background when you're on a phone call. You receive unusual messages, emails, or notifications.


1 Answers

Credit to BvdBijl's idea, I made an extension method to delete the existing one if found and then add it.

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;
            }
        }
    }
}
like image 121
tia Avatar answered Oct 12 '22 23:10

tia