2017-05-04 108 views
1

我有一個應用程序,用戶可以在其中輸入消息。在加載時,應用程序應該進行API調用以從dictionary.com獲取當天的單詞。當用戶鍵入該單詞時,從android發佈消息到AWS SNS主題

將用戶輸入的現有文本發佈到AWS SNS主題。如果用戶輸入名稱,請在郵件中包含名稱。我怎樣才能做到這一點

+0

您想發送消息給AWS主題? –

+0

是的。我想這樣做 – mamshad

+0

讓我與你分享代碼 –

回答

1

首先有你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中找到,它還解釋瞭如何在控制檯中發現問題時獲取憑據。

希望這會有所幫助

+0

非常有幫助。解決了我的問題 – mamshad

+0

很高興它解決了你的問題:) –