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

Jan*_* F. 5 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;
}
Run Code Online (Sandbox Code Playgroud)

这有样品吗?我也在寻找一个认证样本.使用firebase会更容易吗?

谢谢

cub*_*buk 5

我用过GCMSNS发送通知.以下是我已经完成的步骤,假设您已经设置GCM并添加了以下所需的库AWS React Native SDK:

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

在此输入图像描述

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

在此输入图像描述

然后创建你的游泳池,别忘了检查 Enable Access to unauthenticated identities 在此输入图像描述

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

在此输入图像描述

完成这些步骤后,您将能够使用Amazon的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();
Run Code Online (Sandbox Code Playgroud)