Last active
August 29, 2015 14:25
-
-
Save bitsmuggler/f0fc36e4bcfca7b86920 to your computer and use it in GitHub Desktop.
Create Amazon SNS Platform Endpoint in a specific Platform Application
This file contains hidden or 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
private final String gcmArn = "<<your gcm arn>>"; | |
private final String apnsSandboxArn = "<<your apns sandbox arn>>"; | |
private final String apnsArn = "<<your apns arn>>"; | |
private final String plattformNameAndroid = "androidos"; | |
private final String plattformNameIos = "ios"; | |
/** | |
* Creates a platform endpoint by a specific platform and a device-id | |
* @param platformToken Platform specific Push-Notification-Id of the device | |
* @param platform Platform of the device (currently Android or iOS) | |
* @param deviceName Hardware-Id of the device | |
* @return The arn of the platform endpoint | |
*/ | |
private String createPlatformEndpoint(String platformToken, String platform, String deviceName) { | |
CreatePlatformEndpointResult platformEndpointResult = null; | |
if (platform.toLowerCase().equals(plattformNameAndroid)) { | |
platformEndpointResult = createEndpoint(platformToken, Platform.GCM, deviceName); | |
} else if (platform.toLowerCase().equals(plattformNameIos)) { | |
platformEndpointResult = createEndpoint(platformToken, Platform.APNS, deviceName); | |
} | |
if (platformEndpointResult != null) { | |
return platformEndpointResult.getEndpointArn(); | |
} | |
return ""; | |
} | |
/** | |
* Requests the platform endpoint by the platform application arn | |
* @param platformToken Platform specific Push-Notification-Id of the device | |
* @param platform Platform of the device | |
* @param customData Internal Name of the device (administration of devices in amazon sns) | |
* @return Result of the request | |
*/ | |
private CreatePlatformEndpointResult createEndpoint( | |
String platformToken, | |
Platform platform, | |
String customData) { | |
//Getting Platform Application ARN | |
String arn = this.getArn(platform); | |
CreatePlatformEndpointResult platformEndpointResult;// Create an Endpoint. This corresponds to an app on a device. | |
platformEndpointResult = requestPlatformEndpoint( | |
customData, | |
platformToken, arn); | |
return platformEndpointResult; | |
} | |
/** | |
* Gets the ARN of the specfied platform application | |
* @param platform Platform of the device | |
* @return ARN of the specific platform application | |
*/ | |
private String getArn(Platform platform) { | |
if (platform.equals(Platform.APNS)) { | |
return this.apnsArn; | |
} | |
if (platform.equals(Platform.APNS_SANDBOX)) { | |
return this.apnsSandboxArn; | |
} | |
if (platform.equals(Platform.GCM)) { | |
return this.gcmArn; | |
} | |
LOGGER.warn("There isn't any registered arn for this platform."); | |
throw new IllegalArgumentException(); | |
} | |
/** | |
* Requests the creation of the platform endpoint | |
* @param customData Name of the platform endpoint in sns | |
* @param platformToken Platform specific Push-Notification-Id of the device | |
* @param applicationArn ARN of the platform application | |
* @return | |
*/ | |
private CreatePlatformEndpointResult requestPlatformEndpoint( | |
String customData, String platformToken, | |
String applicationArn) { | |
CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest(); | |
platformEndpointRequest.setCustomUserData(customData); | |
String token = platformToken; | |
platformEndpointRequest.setToken(token); | |
platformEndpointRequest.setPlatformApplicationArn(applicationArn); | |
return snsClient.createPlatformEndpoint(platformEndpointRequest); | |
} | |
private enum Platform { | |
// Apple Push Notification Service | |
APNS, | |
// Sandbox version of Apple Push Notification Service | |
APNS_SANDBOX, | |
// Amazon Device Messaging | |
ADM, | |
// Google Cloud Messaging | |
GCM, | |
// Baidu CloudMessaging Service | |
BAIDU, | |
// Windows Notification Service | |
WNS, | |
// Microsoft Push Notificaion Service | |
MPNS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment