首页 > 解决方案 > Amazon 简单通知服务 (SNS) - 如何发布消息以通过其 arn 或令牌指定端点

问题描述

我正在开发一个将通知推送到移动设备的系统,我正在构建一个 asp.net web api 应用程序作为服务器,它将移动设备注册到 aws sns 作为平台应用程序的端点,其令牌来自 Firebase 按摩服务,我已经使用 aws sns 控制台成功地将消息发布到移动设备,但是我找不到任何可以指导我使用 asp.net 服务器实现它的文档。我已经完成了注册并从 aws 控制台创建了端点部分像这样的文件:

    [HttpPost]
    public void RegisterWithSNS(TokenAuth tokenAuth)
    {
        String endpointArn = EndpointArn;
        String applicationArn = "arn:aws:sns:ap-southeast-1:my arn from platform application console";

        bool updateNeeded = false;
        bool createNeeded = (null == endpointArn);

        if (createNeeded)
        {
            // No platform endpoint ARN is stored; need to call createEndpoint.
            EndpointArn = CreateEndpoint(tokenAuth.token, applicationArn);
            createNeeded = false;
        }

        Console.WriteLine("Retrieving platform endpoint data...");
        // Look up the platform endpoint and make sure the data in it is current, even if
        // it was just created.
        try
        {
            GetEndpointAttributesRequest geaReq = new GetEndpointAttributesRequest();
            geaReq.EndpointArn = EndpointArn;
            GetEndpointAttributesResponse geaRes = client.GetEndpointAttributes(geaReq);
            updateNeeded = !(geaRes.Attributes["Token"] == tokenAuth.token) || !(geaRes.Attributes["Enabled"] == "true");
        }
        catch (NotFoundException)
        {
            // We had a stored ARN, but the platform endpoint associated with it
            // disappeared. Recreate it.
            createNeeded = true;
        }

        if (createNeeded)
        {
            CreateEndpoint(tokenAuth.token, applicationArn);
        }

        Console.WriteLine("updateNeeded = " + updateNeeded);

        if (updateNeeded)
        {
            // The platform endpoint is out of sync with the current data;
            // update the token and enable it.
            Console.WriteLine("Updating platform endpoint " + endpointArn);
            Dictionary<String, String> attribs = new Dictionary<String, String>();
            attribs["Token"] = tokenAuth.token;
            attribs["Enabled"] = "true";
            SetEndpointAttributesRequest saeReq = new SetEndpointAttributesRequest();
            saeReq.EndpointArn = EndpointArn;
            saeReq.Attributes = attribs;
            client.SetEndpointAttributes(saeReq);
        }
    }

    private String CreateEndpoint(String token, String applicationArn)
    {
        String endpointArn = null;

        try
        {
            Console.WriteLine("Creating platform endpoint with token " + token);
            CreatePlatformEndpointRequest cpeReq = new CreatePlatformEndpointRequest();
            cpeReq.PlatformApplicationArn = applicationArn;
            cpeReq.Token = token;
            CreatePlatformEndpointResponse cpeRes = client.CreatePlatformEndpoint(cpeReq);
            endpointArn = cpeRes.EndpointArn;
        }
        catch (InvalidParameterException ipe)
        {
            String message = ipe.Message;
            Console.WriteLine("Exception message: " + message);
            Regex rgx = new Regex(".*Endpoint (arn:aws:sns[^ ]+) already exists with the same [Tt]oken.*",
                RegexOptions.IgnoreCase);
            MatchCollection m = rgx.Matches(message);
            if (m.Count > 0 && m[0].Groups.Count > 1)
            {
                // The platform endpoint already exists for this token, but with
                // additional custom data that createEndpoint doesn't want to overwrite.
                // Just use the existing platform endpoint.
                endpointArn = m[0].Groups[1].Value;
            }
            else
            {
                // Rethrow the exception, the input is actually bad.
                throw ipe;
            }
        }
        EndpointArn = endpointArn;
        return endpointArn;
    }        

在移动设备(android)上,我还实现了“从 firebase 获取令牌”并在我的服务器 api 上调用 RegisterWithSNS(TokenAuth tokenAuth),但现在我不知道如何使用端点令牌将消息发布到指定端点或阿恩。谁能告诉我怎么做?

标签: androidasp.netamazon-web-servicesamazon-sns

解决方案


基于@John Rotenstein 对我的问题的评论,我自己实现了它,就像下面这样:

    public bool PublishMessage(AWSClientModel aWSClientModel) { 
           PublishRequest publishRequest = new PublishRequest(); 
           publishRequest.Message =JsonConvert.SerializeObject(aWSClientModel.Notification); 
           publishRequest.TargetArn = aWSClientModel.EndpointArn;
           PublishResponse publishResponse = client.Publish(publishRequest); 
           if (publishResponse.HttpStatusCode == System.Net.HttpStatusCode.OK) { 
             return true; 
           } 
          return false; 
     }

推荐阅读