gpt4 book ai didi

amazon-web-services - 如何在 React Native (Android) 中订阅 AWS SNS 主题?

转载 作者:行者123 更新时间:2023-12-04 08:02:37 25 4
gpt4 key购买 nike

我尝试使用 aws-sdk-react-native 模块:

https://github.com/awslabs/aws-sdk-react-native

配置需要一些时间,但是由于有了这个链接,我可以列出主题:

https://github.com/awslabs/aws-sdk-react-native/issues/35

https://github.com/awslabs/aws-sdk-react-native/blob/master/SNS/IntegrationTests/SNSTests.js

该测试包括一个示例如何订阅以获取电子邮件,但不包括如何在应用程序中获取通知。我不知道如何获取 platformEndpoint、PlatformApplicationArn 和 deviceToken。

endPoint = sns.createPlatformEndpoint({
PlatformApplicationArn: '{APPLICATION_ARN}',
Token: '{DEVICE_TOKEN}'
})
...
var subscribeRequest= {
"Protocol":"application",
"TopicArn":topicARN,
"Endpoint":endPoint
}
try{
await AWSSNS.Subscribe(subscribeRequest);
}catch(e){
console.error(e);
shouldResolve = false;
return shouldResolve;
}

有这方面的 sample 吗?
我也在寻找一个认证样本。
使用firebase会更容易吗?

谢谢

最佳答案

我用过GCM通过 SNS 发送通知。以下是我假设您已经设置 GCM 所经历的步骤并从 AWS React Native SDK 添加了所需的库:

首先创建一个SNS来自 AWS 的应用程序:

enter image description here

然后你需要通过Cognito创建Federated Identity AWS 的服务。这是将您的设备 token 从移动应用程序发送到 AWS SNS 应用程序所必需的。选择Manage Federated Identities
enter image description here

然后创建您的池,不要忘记检查 Enable Access to unauthenticated identities enter image description here

创建池时,您需要为 unauthenticated 创建 IAM 角色和 authenticated该池的角色。 AWS 将帮助您为此创建新角色,但您需要转到 IAM Roles菜单并附上AmazonSNSFullAccess到创建的角色,否则您将无法从移动应用程序发送设备 token 。

enter image description here

完成这些步骤后,您将能够使用 Amazon 的 React Native SDK 发送您的设备 token 。我已经按照此处的建议编写了一个用于将 token 发送到 AWS SNS 的帮助程序类:

class AWSUtility {

constructor() {
const region = "us-west-1"; //change it with your region
const IDENTITY_POOL_ID = "pool id created from Federated Identities"
AWSCognitoCredentials.initWithOptions({region, identity_pool_id: IDENTITY_POOL_ID});
AWSSNS.initWithOptions({region});
}

addTokenToAWSSNS(token, snsEndpointARN) {
const applicationArn = "change with SNS application Amazon resource name";
return Promise.try(() => {
if (!snsEndpointARN) {
return this.createPlatformEndpoint(token, applicationArn);
} else {
return AWSSNS.GetEndpointAttributes({EndpointArn: snsEndpointARN})
.then((result) => {
const {Attributes = {}} = result;
const {Token, Enabled} = Attributes;
const updateNeeded = Token !== token || Enabled !== 'true';
if (updateNeeded) {
return this.updateEndpoint(token).then(() => result.EndpointArn);
}
return snsEndpointARN;
})
.catch(() => {
this.createPlatformEndpoint(token, applicationArn)
});
}
});
}

updateEndpoint(snsEndpointARN, token) {
//AWS is returning error saying that it requires 6 params to update endpoint, if anyone has any idea about it let me know please
return AWSSNS.SetEndpointAttributes({EndpointArn: snsEndpointARN, Attributes: {Token: token, Enabled: true}});
}

createPlatformEndpoint(token, applicationArn) {
return AWSSNS.CreatePlatformEndpoint({Token: token, PlatformApplicationArn: applicationArn})
.then(result => result.EndpointArn)
.catch((error = {}) => {
console.log(error);
});
}
}

export default new AWSUtility();

关于amazon-web-services - 如何在 React Native (Android) 中订阅 AWS SNS 主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com