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

时间:2017-02-23 08:55:42

标签: amazon-web-services amazon-sns react-native-android

我尝试使用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;
}

有没有这样的样品? 我也在寻找认证样本。 使用firebase会更容易吗?

由于

1 个答案:

答案 0 :(得分:5)

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

首先从AWS创建一个SNS应用:

enter image description here

然后,您需要通过AWS的Cognito服务创建联合身份。这是将设备令牌从移动应用程序发送到AWS SNS应用程序所必需的。选择Manage Federated Identities

enter image description here

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

创建池时,您需要为该池的unauthenticatedauthenticated角色创建IAM角色。 AWS将帮助您为此创建新角色,但您需要转到IAM Roles菜单并将AmazonSNSFullAccess附加到已创建的角色,否则您将无法通过移动应用程序发送设备令牌。< / p>

enter image description here

完成这些步骤后,您就可以使用亚马逊的React Native SDK发送设备令牌。我编写了一个帮助类,用于将令牌发送到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();