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

问题描述 投票:0回答:4

我正在使用适用于 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.
的错误。如何检查端点是否已存在,更重要的是,如何获取给定令牌的端点?

ruby amazon-web-services amazon-sns
4个回答
13
投票

归功于 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;
            }
        }
    }
}

0
投票

亚马逊似乎解决了这个问题。 我正在使用 RoR,并且在尝试注册和现有 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 代码(具有与原始代码相同的属性)时,我收到端点 arn,而不是错误消息。


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
投票

如果有人仍在研究此问题,正确的方法是保存与您的用户模型关联的端点 ARN 并根据该值删除端点,然后重新创建。我还保存对主题的订阅并取消订阅。随着用户群的增长,循环遍历所有端点是一个非常糟糕的主意。

© www.soinside.com 2019 - 2024. All rights reserved.