aws-amplify - 如何使用 Amazon Amplify SDK 以编程方式为我的 Android 应用程序用户发送短信?

标签 aws-amplify aws-amplify-sdk-android

当我的应用用户使用他们的电话号码注册时,我想向他们的电话号码发送欢迎消息 (SMS)。我找不到有关此特定任务的官方文档。

最佳答案

Amazon 让您可以做到这一点。假设您使用 Cognito 进行注册,您将需要使用确认后 Cognito lambda 触发器。

  1. 通过 AWS 控制台设置您的 SNS 账户,以发送 SMS 消息。通过控制台向自己发送一条测试消息。

  2. 运行amplify auth update

  3. 当遇到问题Do you want to configure Lambda Triggers for Cognito?时,回答Yes并选择Post Confirmation 触发

  4. 您需要向 lambda 授予 SNS (SMS) 权限。更新 PostConfirmation-cloudformation-template.json 文件以在 Resources.lambdaexecutionpolicy.Properties.PolicyDocument.Statement 下添加新语句:

    {
    "Resources": {
        "lambdaexecutionpolicy": {
            "Properties": {
                "PolicyDocument": {
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": "sns:*",
                            "Resource": "*"
                        }
                    ]
                    ...
                }
            ...
            }
        ...
        }  
    ...
    }
    ...
    }
    
  5. 将此代码用于触发器:

    var aws = require('aws-sdk');
    
    var sms = new aws.SNS();
    
    exports.handler = (event, context, callback) => {
      console.log(event);
    
      if (event.request.userAttributes.phone_number) {
        sendSMS(event.request.userAttributes.phone_number, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) {
    
          // Return to Amazon Cognito
          callback(null, event);
        });
      } else {
        // Nothing to do, the user's phone number is unknown
        callback(null, event);
      }
    };
    
    function sendSMS(to, message, completedCallback) {
      const params = {
        Message: message, /* required */
        PhoneNumber: to
      };
      sns.publish(params, function (err, data) {
        if (err) {
          console.log(err, err.stack); // an error occurred
        } else {
          console.log(data);
        }
        completedCallback("SMS Sent");
      })
    };
    

关于aws-amplify - 如何使用 Amazon Amplify SDK 以编程方式为我的 Android 应用程序用户发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60430828/

相关文章:

android - amplify-gradle-config.json(系统找不到指定的文件)

javascript - AWS 放大自定义 header : Property Assignment Expected

aws-amplify - AWS Amplify 创建多个具有重复信息的 DynamoDB 表?

android - 使用 AWS DataStore 的完全离线选项,然后允许在 Android 中选择激活云同步功能

cors - 放大 : CORS header ‘Access-Control-Allow-Origin’ missing error even though CORS is enabled in API Gateway and Lambda headers

android - 放大身份验证 (Cognito) - 登录期间的 "Error in federating the token"

android - AWS 移动开发工具包库会因为 Amplify 而消失吗?

amazon-web-services - AWS Amplify 身份验证错误

java - 如何使用 Android Sdk 在 aws S3 存储桶中创建空文件夹?