No Description

SNSRegister.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package uprrp.tania;
  2. import android.os.AsyncTask;
  3. import android.util.Log;
  4. import com.amazonaws.auth.CognitoCachingCredentialsProvider;
  5. import com.amazonaws.services.sns.AmazonSNSClient;
  6. import com.amazonaws.services.sns.model.CreatePlatformEndpointRequest;
  7. import com.amazonaws.services.sns.model.CreatePlatformEndpointResult;
  8. import com.amazonaws.services.sns.model.GetEndpointAttributesRequest;
  9. import com.amazonaws.services.sns.model.GetEndpointAttributesResult;
  10. import com.amazonaws.services.sns.model.InvalidParameterException;
  11. import com.amazonaws.services.sns.model.NotFoundException;
  12. import com.amazonaws.services.sns.model.SetEndpointAttributesRequest;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. public class SNSRegister extends AsyncTask <String, Void, String> {
  18. private static final String TAG = "SNSRegister";
  19. AmazonSNSClient client;
  20. String arnStorage = null;
  21. public SNSRegister (CognitoCachingCredentialsProvider credentialsProvider){
  22. client = new AmazonSNSClient(credentialsProvider);
  23. }
  24. @Override
  25. protected void onPostExecute(String s) {
  26. super.onPostExecute(s);
  27. }
  28. @Override
  29. protected String doInBackground(String... strings) {
  30. String endpointArn = retrieveEndpointArn();
  31. String token = GlobalValues.getInstance().getDeviceToken(); // FirebaseInstanceId.getInstance().getToken()
  32. boolean updateNeeded = false;
  33. boolean createNeeded = (null == endpointArn);
  34. if (createNeeded) {
  35. // No platform endpoint ARN is stored; need to call createEndpoint.
  36. endpointArn = createEndpoint(token);
  37. createNeeded = false;
  38. }
  39. Log.i(TAG, "Retrieving platform endpoint data...");
  40. // Look up the platform endpoint and make sure the data in it is current, even if
  41. // it was just created.
  42. try {
  43. GetEndpointAttributesRequest geaReq =
  44. new GetEndpointAttributesRequest()
  45. .withEndpointArn(endpointArn);
  46. GetEndpointAttributesResult geaRes =
  47. client.getEndpointAttributes(geaReq);
  48. updateNeeded = !geaRes.getAttributes().get("Token").equals(token)
  49. || !geaRes.getAttributes().get("Enabled").equalsIgnoreCase("true");
  50. } catch (NotFoundException nfe) {
  51. // We had a stored ARN, but the platform endpoint associated with it
  52. // disappeared. Recreate it.
  53. createNeeded = true;
  54. }
  55. if (createNeeded) {
  56. createEndpoint(token);
  57. }
  58. Log.d(TAG, "updateNeeded = " + updateNeeded);
  59. if (updateNeeded) {
  60. // The platform endpoint is out of sync with the current data;
  61. // update the token and enable it.
  62. Log.d(TAG, "Updating platform endpoint " + endpointArn);
  63. Map attribs = new HashMap();
  64. attribs.put("Token", token);
  65. attribs.put("Enabled", "true");
  66. SetEndpointAttributesRequest saeReq =
  67. new SetEndpointAttributesRequest()
  68. .withEndpointArn(endpointArn)
  69. .withAttributes(attribs);
  70. client.setEndpointAttributes(saeReq);
  71. }
  72. return endpointArn;
  73. }
  74. /**
  75. * @return never null
  76. * */
  77. private String createEndpoint(String token) {
  78. String endpointArn = null;
  79. try {
  80. Log.d(TAG, "Creating platform endpoint with token " + token);
  81. CreatePlatformEndpointRequest cpeReq =
  82. new CreatePlatformEndpointRequest()
  83. .withPlatformApplicationArn("arn:aws:sns:us-east-1:227586183436:app/GCM/TANIA_Android")
  84. .withToken(token);
  85. CreatePlatformEndpointResult cpeRes = client
  86. .createPlatformEndpoint(cpeReq);
  87. endpointArn = cpeRes.getEndpointArn();
  88. } catch (InvalidParameterException ipe) {
  89. String message = ipe.getErrorMessage();
  90. Log.d(TAG, "Exception message: " + message);
  91. Pattern p = Pattern
  92. .compile(".*Endpoint (arn:aws:sns[^ ]+) already exists " +
  93. "with the same token.*");
  94. Matcher m = p.matcher(message);
  95. if (m.matches()) {
  96. // The platform endpoint already exists for this token, but with
  97. // additional custom data that
  98. // createEndpoint doesn't want to overwrite. Just use the
  99. // existing platform endpoint.
  100. endpointArn = m.group(1);
  101. } else {
  102. // Rethrow the exception, the input is actually bad.
  103. throw ipe;
  104. }
  105. }
  106. storeEndpointArn(endpointArn);
  107. return endpointArn;
  108. }
  109. private String retrieveEndpointArn() {
  110. return arnStorage;
  111. }
  112. private void storeEndpointArn(String endpointArn) {
  113. arnStorage = endpointArn;
  114. }
  115. }