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

≯℡__Kan透↙ 提交于 2019-12-04 03:15:47

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;
            }
        }
    }
}

It looks like amazone resolved this issue. I'm using RoR and used to have same problem when trying to register and existing GCM code I got an error message saying

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

although I used same (empty) attributes. Now when I send an existing GCM code (with same attributes as the original one) I get the endpoint arn and not the error message.

ListEndpointsByPlatformApplication only return 100 endpoints, you have to use nextToken to get more. Here is my implementation.

    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);

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!