【问题标题】:Amazon simple notification service (SNS) - How to publish a message to specify endpoint by its arn or tokenAmazon 简单通知服务 (SNS) - 如何发布消息以通过其 arn 或令牌指定端点
【发布时间】:2020-08-30 19:37:10
【问题描述】:

我正在开发一个将通知推送到移动设备的系统,我正在构建一个 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),但现在我不知道如何通过以下方式将消息发布到指定端点使用端点令牌或 arn。 谁能告诉我怎么做?

【问题讨论】:

  • 我想你可以打电话给Publish()并指定一个TargetArn:Publish - Amazon Simple Notification Service
  • @John Rotenstein:我会试一试,我认为它会工作
  • @JohnRotenstein :它的工作,你应该把你的评论作为答案
  • 这就是我实现它的方式: 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; } 返回假; }
  • 欢迎您为自己的问题添加答案。 (这样也更好看!)

标签: android asp.net amazon-web-services amazon-sns


【解决方案1】:

基于@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; 
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2014-01-23
    • 2015-11-18
    相关资源
    最近更新 更多