我有一个应用程序,用户可以在其中输入消息。在加载时,应用程序应该进行 API 调用以从 dictionary.com 获取当天的单词。当用户输入那个词时,
将用户输入的现有文本发布到 AWS SNS 主题。如果用户输入了姓名,则在消息中包含该姓名。我该怎么做
最佳答案
首先从控制台获得您的 AWS 凭证
// AWS Iot CLI describe-endpoint call returns: XXXXXXXXXX.iot.<region>.amazonaws.com
private static final String CUSTOMER_SPECIFIC_ENDPOINT = "Your Specific End Point here";
// Cognito pool ID. For this app, pool needs to be unauthenticated pool with
// AWS IoT permissions.
private static final String COGNITO_POOL_ID = "Your Pool id here e.g us-west-2:7eed0766-71bb-4802";
// Name of the AWS IoT policy to attach to a newly created certificate
private static final String AWS_IOT_POLICY_NAME = "iotPubSub";
// Region of AWS IoT
private static final Regions MY_REGION = Regions.US_WEST_2;
// Filename of KeyStore file on the filesystem
private static final String KEYSTORE_NAME = "iot_keystore";
// Password for the private key in the KeyStore
private static final String KEYSTORE_PASSWORD = "password";
// Certificate and key aliases in the KeyStore
private static final String CERTIFICATE_ID = "default";
然后在onCreate()
credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), // context
COGNITO_POOL_ID, // Identity Pool ID
MY_REGION // Region
);
Region region = Region.getRegion(MY_REGION);
// MQTT Client
mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
// Set keepalive to 10 seconds. Will recognize disconnects more quickly but will also send
// MQTT pings every 10 seconds.
mqttManager.setKeepAlive(10);
AWSIotMqttLastWillAndTestament lwt = new
AWSIotMqttLastWillAndTestament("my/lwt/topic",
"Android client lost connection", AWSIotMqttQos.QOS0);
mqttManager.setMqttLastWillAndTestament(lwt);
// IoT Client (for creation of certificate if needed) 0316 5533564 873459
mIotAndroidClient.setRegion(region);
keystorePath = getFilesDir().getPath();
现在简单地用您的主题名称调用电话,完整代码可以在github中找到,它还说明了如果您在控制台中找不到凭据时如何获取凭据。
希望对你有帮助
关于android - 将消息从 android 发布到 AWS SNS 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43778977/