亚马逊短信推送服务

参考文档

https://docs.aws.amazon.com/zh_cn/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk

新建配置文件 AwsCredentialsSMS.properties

参数:accessKeysecretKey (从亚马逊账号中找)

sbt

"software.amazon.awssdk" % "s3" % "2.0.0-preview-5" % Test

导包

import com.amazonaws.auth.PropertiesCredentials
import com.amazonaws.services.sns.model.{MessageAttributeValue, PublishRequest, PublishResult}
import com.amazonaws.services.sns.{AmazonSNS, AmazonSNSClient}
import components.amazonaws.AmazonSNSClientWrapper

补充:AmazonSNSClientWrapper

来自以下文档(步骤4中的snsmobilepush.zip):
https://docs.aws.amazon.com/sns/latest/dg/mobile-push-gcm.html

参考代码

object AmazonSMS {

  val snsClient: AmazonSNS = new AmazonSNSClient(new PropertiesCredentials(classOf[AmazonSNSClientWrapper].getResourceAsStream("AwsCredentialsSMS.properties")))
  snsClient.setEndpoint("https://sns.us-west-2.amazonaws.com")

  val smsAttributes: util.HashMap[String, MessageAttributeValue] = new util.HashMap[String, MessageAttributeValue]
  smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue().withStringValue("10.0").withDataType //Sets the max price to 0.50 USD.
    ("Number"))
  smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Promotional").withDataType //Sets the type to promotional.
    ("String"))

  def getAmazonSMS: AmazonSMS = AmazonSMS(snsClient, smsAttributes)

}

case class AmazonSMS(snsClient: AmazonSNS, smsAttributes: util.HashMap[String, MessageAttributeValue]) {

  def publish(message: String, phone: String)(implicit ec: ExecutionContext): Future[PublishResult] =  Future{
    val result = snsClient.publish(new PublishRequest().withMessage(message).withPhoneNumber(phone).withMessageAttributes(smsAttributes))
    println(result)
    result
  }

}

AmazonSMS.getAmazonSMS.publish(code, mobile)

常见问题补充

  • 亚马逊账号短信服务开通地是什么,snsClient.setEndpoint就是什么,地区保持一致
  • AWS.SNS.SMS.MaxPrice 默认为1$,想要提高额度,需要向亚马逊提交申请

亚马逊app推送,以后在更

你可能感兴趣的:(亚马逊短信推送服务)