如何检查移动设备是否已注册

Bvd*_*ijl 8 ruby amazon-web-services amazon-sns

我正在使用适用于Amazon SNS的Amazon AWS Ruby SDK,但我在使用已经注册的设备时遇到了一些问题.有时当设备再次注册时,我会收到类似的错误AWS::SNS::Errors::InvalidParameter Invalid parameter: Token Reason: Endpoint arn:aws:sns:us-east-1:**** already exists with the same Token, but different attributes..如何检查端点是否已存在,更重要的是,如何获取给定令牌的端点?

tia*_*tia 11

感谢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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 太疯狂了,这实际上是我能看到的唯一方式. (3认同)
  • 您可以调用:SetEndpointAttributes,而不是删除和创建平台端点,其属性为"Enabled = true" (2认同)
  • 必须从将来可能会改变的错误消息中解析出这一点,感觉很奇怪。有点可笑的是,他们不只是将重复的端点返回给我们。 (2认同)