Created
February 17, 2016 00:32
-
-
Save chocksaway/17ad9afcebfeaa7e06a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chocksaway; | |
import com.amazonaws.auth.PropertiesCredentials; | |
import com.amazonaws.regions.Region; | |
import com.amazonaws.regions.Regions; | |
import com.amazonaws.services.sns.AmazonSNS; | |
import com.amazonaws.services.sns.AmazonSNSClient; | |
import com.amazonaws.services.sns.model.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* Created by milesd on 16/02/2016. | |
*/ | |
public class SNSUtil { | |
public AmazonSNS getSNSClientFromAWSCredentials() throws IOException { | |
File securityFile = new File("AwsCredentials.properties"); | |
PropertiesCredentials credentials = new PropertiesCredentials(securityFile); | |
AmazonSNS snsClient = new AmazonSNSClient(credentials); | |
snsClient.setRegion(Region.getRegion(Regions.EU_WEST_1)); | |
return snsClient; | |
} | |
/*** | |
* | |
* @param applicationName | |
* @param platform | |
* @param credential | |
* @return | |
* | |
* PlatformPrincipal = GCM | |
* PlatformCredential = api id | |
* applicationName = demoApp | |
* platform.name = GCM | |
*/ | |
private CreatePlatformApplicationResult createPlatformApplicationArn(String applicationName, | |
String platform, | |
String credential, | |
AmazonSNS snsClient) { | |
CreatePlatformApplicationRequest platformApplicationRequest = new CreatePlatformApplicationRequest(); | |
Map<String, String> attributes = new HashMap<String, String>(); | |
attributes.put("PlatformPrincipal", platform); | |
attributes.put("PlatformCredential", credential); | |
platformApplicationRequest.setAttributes(attributes); | |
platformApplicationRequest.setName(applicationName); | |
platformApplicationRequest.setPlatform(platform); | |
return snsClient.createPlatformApplication(platformApplicationRequest); | |
} | |
private CreatePlatformEndpointResult createPlatformEndpoint( | |
AmazonSNS snsClient, | |
String customData, String platformToken, | |
String applicationArn) { | |
CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest(); | |
platformEndpointRequest.setCustomUserData(customData); | |
platformEndpointRequest.setToken(platformToken); | |
platformEndpointRequest.setPlatformApplicationArn(applicationArn); | |
return snsClient.createPlatformEndpoint(platformEndpointRequest); | |
} | |
private Topic getTopicArns(AmazonSNS snsClient, String topicName) { | |
List<Topic> topicArns = new ArrayList<Topic>(); | |
ListTopicsResult result = snsClient.listTopics(); | |
topicArns.addAll(result.getTopics()); | |
while (result.getNextToken() != null) { | |
result = snsClient.listTopics(result.getNextToken()); | |
topicArns.addAll(result.getTopics()); | |
} | |
for (Topic topic : topicArns) { | |
System.out.println(topic.getTopicArn()); | |
if (topic.getTopicArn().contains(topicName)) | |
return topic; | |
} | |
return null; | |
} | |
public static void main(String[] args) throws IOException { | |
// think async client | |
System.out.println("tester"); | |
SNSUtil snsUtils = new SNSUtil(); | |
AmazonSNS snsClient = snsUtils.getSNSClientFromAWSCredentials(); | |
/** | |
GCM | |
PlatformCredential = <snip> | |
applicationName = demoApp | |
platform.name = GCM | |
**/ | |
CreatePlatformApplicationResult platformResult = snsUtils.createPlatformApplicationArn("demoApp", | |
"GCM", | |
"snip", | |
snsClient); | |
String platformApplicationArn = platformResult.getPlatformApplicationArn(); | |
String registrationId = "snip" | |
CreatePlatformEndpointResult platformEndpointResult = snsUtils.createPlatformEndpoint(snsClient, "", registrationId, platformApplicationArn); | |
Topic topic = snsUtils.getTopicArns(snsClient, "News"); | |
SubscribeRequest sr = new SubscribeRequest(); | |
sr.setProtocol("endpoint"); | |
sr.setTopicArn(topic.getTopicArn()); | |
sr.setEndpoint(platformApplicationArn); | |
SubscribeResult subscribeResult = snsClient.subscribe(sr); | |
System.out.println("tester"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment