【问题标题】:Send push notification to a specific endpoint向特定端点发送推送通知
【发布时间】:2015-04-13 13:44:53
【问题描述】:
我想使用亚马逊 SNS 将推送通知从 android 应用程序直接发送到 android 端点。如果我只有端点的 GCM 令牌,我想知道该怎么做(如果不可能,我必须这样做吗?)。如果有必要,我还必须知道如何从端点应用程序在亚马逊 SNS 上注册端点。我正在使用安卓工作室。我是应用程序开发的新手,需要这个尽快!
谢谢
安德烈
【问题讨论】:
-
享受docs。有示例、示例以及许多您正在寻找的东西。
标签:
push-notification
google-cloud-messaging
amazon-sns
【解决方案1】:
您可以使用以下方法从android设备注册亚马逊sns。在您注册之前,您必须获得
1.access_key_id
2.secret_access_key
3.application Arn
4.来自gcm的API密钥
然后使用以下代码:
private String createEndpoint(String token) {
String endpointArn = null;
try {
System.out.println("Creating endpoint with token " + token);
CreatePlatformEndpointRequest cpeReq = new CreatePlatformEndpointRequest().withPlatformApplicationArn(applicationArn).withToken(token);
CreatePlatformEndpointResult cpeRes = client.createPlatformEndpoint(cpeReq);
endpointArn = cpeRes.getEndpointArn();
} catch (InvalidParameterException ipe) {
String message = ipe.getErrorMessage();
System.out.println("Exception message: " + message);
Pattern p = Pattern.compile(".*Endpoint (arn:aws:sns[^ ]+) already exists " + "with the same Token.*");
Matcher m = p.matcher(message);
if (m.matches()) {
// the endpoint already exists for this token, but with
// additional custom data that
// CreateEndpoint doesn't want to overwrite. Just use the
// existing endpoint.
endpointArn = m.group(1);
} else {
// rethrow exception, the input is actually bad
throw ipe;
}
}
storeEndpointArn(endpointArn);
return endpointArn;
}