将推送通知发送到特定端点

时间:2015-04-13 13:44:54

标签: push-notification google-cloud-messaging amazon-sns

我想使用亚马逊SNS从Android应用程序直接向Android端点发送推送通知。如果我只有端点的GCM令牌,我想知道如何做到这一点(如果不可能,我必须这样做?)。如果有必要,我必须知道如何从端点应用程序在亚马逊SNS上注册端点。我正在使用Android Studio。我是应用程序开发的新手,需要尽快!

谢谢 安德烈

1 个答案:

答案 0 :(得分:1)

你可以使用以下方法从亚马逊设备注册亚马逊sns。在你去注册之前你必须得到 1.access_key_id 2.secret_access_key 3.应用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;
        }
相关问题