Trying to create Endpoints using ARN and device token. For generating the endpoints I have used the below class in Android Application. I am not receiving notification from AWS SNS, is there anything that I'm missing to add in the code?
To generate device token I have added this line.
var token = FirebaseInstanceId.getInstance().token
class EndpointRegistration {
companion object {
var client = AmazonSNSClient() //provide credentials here
val awsCredentials: AWSCredentials = BasicAWSCredentials(BuildConfig.AWS_ACCESS, BuildConfig.AWS_SECRET_KEY)
var arnStorage: String? = null
fun registerWithSNS(userToken: String, context: Context) {
var endpointArn: String? = retrieveEndpointArn()
val token = userToken
var updateNeeded = false
var createNeeded = null == endpointArn
if (createNeeded) {
// No platform endpoint ARN is stored; need to call createEndpoint.
endpointArn = createEndpoint(context, token)
createNeeded = false
}
println("Retrieving platform endpoint data...")
// Look up the platform endpoint and make sure the data in it is current, even if
// it was just created.
try {
val geaReq = GetEndpointAttributesRequest()
.withEndpointArn(endpointArn)
val geaRes: GetEndpointAttributesResult = client.getEndpointAttributes(geaReq)
updateNeeded = (geaRes.attributes["Token"] != token
|| !geaRes.attributes["Enabled"].equals("true", ignoreCase = true))
} catch (nfe: NotFoundException) {
// We had a stored ARN, but the platform endpoint associated with it
// disappeared. Recreate it.
createNeeded = true
}
if (createNeeded) {
createEndpoint(context, token)
}
println("updateNeeded = $updateNeeded")
if (updateNeeded) {
// The platform endpoint is out of sync with the current data;
// update the token and enable it.
println("Updating platform endpoint $endpointArn")
val attribs: MutableMap<String, String> = HashMap()
attribs["Token"] = token
attribs["Enabled"] = "true"
val saeReq = SetEndpointAttributesRequest()
.withEndpointArn(endpointArn)
.withAttributes(attribs)
client.setEndpointAttributes(saeReq)
Log.d("Updatingd", "=" + client.endpointPrefix)
}
}
private fun createEndpoint(context: Context, token: String): String? {
var endpointArn: String? = null
endpointArn = try {
println("Creating platform endpoint with token $token")
client = AmazonSNSClient(awsCredentials)
client.setRegion(Region.getRegion(Regions.EU_WEST_1));
val cpeReq = CreatePlatformEndpointRequest()
.withPlatformApplicationArn(BuildConfig.PLATFORM_ARN)
.withToken(token)
val cpeRes: CreatePlatformEndpointResult = client
.createPlatformEndpoint(cpeReq)
cpeRes.endpointArn
} catch (ipe: InvalidParameterException) {
val message: String = ipe.getErrorMessage()
println("Exception message: $message")
val p: Pattern = Pattern
.compile(".*Endpoint (arn:aws:sns[^ ]+) already exists " +
"with the same [Tt]oken.*")
val m: Matcher = p.matcher(message)
if (m.matches()) {
// The platform endpoint already exists for this token, but with
// additional custom data that
// createEndpoint doesn't want to overwrite. Just use the
// existing platform endpoint.
m.group(1)
} else {
// Rethrow the exception, the input is actually bad.
throw ipe
}
}
storeEndpointArn(endpointArn!!)
return endpointArn
}
/**
* @return the ARN the app was registered under previously, or null if no
* platform endpoint ARN is stored.
*/
private fun retrieveEndpointArn(): String? {
// Retrieve the platform endpoint ARN from permanent storage,
// or return null if null is stored.
return arnStorage
}
/**
* Stores the platform endpoint ARN in permanent storage for lookup next time.
*/
private fun storeEndpointArn(endpointArn: String) {
// Write the platform endpoint ARN to permanent storage.
arnStorage = endpointArn
}
}
}