android - 将消息从 android 发布到 AWS SNS 主题

标签 android

我有一个应用程序,用户可以在其中输入消息。在加载时,应用程序应该进行 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/

相关文章:

java - 反序列化由@JsonIdentityInfo 序列化的对象

java - 安卓 : SharedPreferences how to disapear second parameter from activity (if nothing found in sharedPreferences)

Android - 使用 PIM 和网络估计 BlackBerry 应用程序的端口

android - 自定义键盘 android 中显示褪色的文本

android - 这是一个 ListView ,以及如何做到这一点

android - 有没有办法在 C++ 中以编程方式执行 adb 命令?这部分 C++ 代码是使用 android studio 中的 ndk build 构建的。那里

android - 如何在 Android 中更改 EditTextPreference 的 dialogMessage 的字体?

Android Studio : Rendering Problems Missing styles-correct theme chosen for this layout, 无法找到带有 id 的样式

android - addAccountExplicitly 成功,但未在设置/帐户中显示任何帐户

android - 如何在 Android 中集成 Admob 奖励广告?