Amazon 简单通知服务 (SNS) - 如何发布消息以按其 arn 或令牌指定终端节点



我正在开发一个将通知推送到移动设备的系统,我正在构建一个 asp.net Web API 应用程序作为服务器,该应用程序将使用来自 Firebase 按摩服务的令牌将移动设备注册到 aws sns 作为平台应用程序的终端节点,我已经使用 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将消息发布到指定端点。 谁能告诉我怎么做?

基于@John罗滕斯坦对我问题的评论,我只是像波纹管一样自己实现了它:

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

最新更新