Android没有推送通知,没有使用AWS SNS android接收推送通知

我正在开发使用AWS SNS接收推送通知的Android应用程序 . 基本流程是我从api接收订阅ID,订阅密钥和主题订阅 . 我使用提供的平台应用程序ARN创建 endpoints 并订阅该主题 .

我的代码是:

AmazonSNSClient client;

SharedPreferences userPreferences;

ProfileData profileData;

String token;

@Override

protected Void doInBackground(ARScreen.Container... containers) {

ARScreen.Container container = containers[0];

profileData = container.profileData;

userPreferences = container.userPreferences;

token = container.token;

BasicAWSCredentials credentials = new BasicAWSCredentials(profileData.getSubscribeId(), profileData.getSubscribeSecret());

client = new AmazonSNSClient(credentials);

client.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1));

registerWithSNS(token);

return null;

}

@SuppressLint("CommitPrefEdits")

@SuppressWarnings({"deprecation", "unchecked"})

public void registerWithSNS(String regId) {

String endpointArn = retrieveEndpointArn();

boolean updateNeeded = false;

boolean createNeeded = (null == endpointArn);

if (createNeeded) {

// No platform endpoint ARN is stored; need to call createEndpoint.

endpointArn = createEndpoint(regId);

createNeeded = false;

}

System.out.println("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()

.withEndpointArn(endpointArn);

GetEndpointAttributesResult geaRes =

client.getEndpointAttributes(geaReq);

updateNeeded = !geaRes.getAttributes().get("Token").equals(regId)

|| !geaRes.getAttributes().get("Enabled").equalsIgnoreCase("true");

} catch (NotFoundException nfe) {

// We had a stored ARN, but the platform endpoint associated with it

// disappeared. Recreate it.

createNeeded = true;

}

if (createNeeded) {

createEndpoint(regId);

}

System.out.println("updateNeeded = " + updateNeeded);

if (updateNeeded) {

// The platform endpoint is out of sync with the current data;

// update the token and enable it.

System.out.println("Updating platform endpoint " + endpointArn);

Map attribs = new HashMap();

attribs.put("Token", regId);

attribs.put("Enabled", "true");

SetEndpointAttributesRequest saeReq =

new SetEndpointAttributesRequest()

.withEndpointArn(endpointArn)

.withAttributes(attribs);

client.setEndpointAttributes(saeReq);

}

String subscriptionId = client.subscribe(new SubscribeRequest()

.withEndpoint(endpointArn)

.withProtocol("application")

.withTopicArn(profileData.getSnstopic())

).getSubscriptionArn();

System.out.println("Id" + subscriptionId);

SubscribeRequest subscribeRequest = new SubscribeRequest(profileData.getSnstopic(), "application", endpointArn);

SubscribeResult result = client.subscribe(subscribeRequest);

if (result != null) {

SharedPreferences.Editor editor = userPreferences.edit();

editor.putBoolean("isSubscribed", true);

editor.commit();

}

}

/**

* @return never null

*/

private String createEndpoint(String token) {

String endpointArn;

try {

System.out.println("Creating platform endpoint with token " + token);

CreatePlatformEndpointRequest cpeReq =

new CreatePlatformEndpointRequest()

.withPlatformApplicationArn("My Platform Application ARN")

.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 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.group(1);

} else {

// Rethrow the exception, the input is actually bad.

throw ipe;

}

}

storeEndpointArn(endpointArn);

return endpointArn;

}

/**

* @return the ARN the app was registered under previously, or null if no

* platform endpoint ARN is stored.

*/

private String retrieveEndpointArn() {

// Retrieve the platform endpoint ARN from permanent storage,

// or return null if null is stored.

return userPreferences.getString("endPointArn", null);

}

/**

* Stores the platform endpoint ARN in permanent storage for lookup next time.

*/

@SuppressLint("CommitPrefEdits")

private void storeEndpointArn(String endpointArn) {

// Write the platform endpoint ARN to permanent storage.

SharedPreferences.Editor editor = userPreferences.edit();

editor.putString("endPointArn", endpointArn);

editor.commit();

}

我可以在日志控制台中看到我的订阅ID . 问题是我没有收到与该主题相关的任何推送通知 . 我错过了代码中的一些东西吗?任何帮助,将不胜感激!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值