private PublishResult publish(
      String endpointArn,
      Platform platform,
      Map<Platform, Map<String, MessageAttributeValue>> attributesMap) {
    PublishRequest publishRequest = new PublishRequest();
    Map<String, MessageAttributeValue> notificationAttributes =
        getValidNotificationAttributes(attributesMap.get(platform));
    if (notificationAttributes != null && !notificationAttributes.isEmpty()) {
      publishRequest.setMessageAttributes(notificationAttributes);
    }
    publishRequest.setMessageStructure("json");
    // If the message attributes are not set in the requisite method,
    // notification is sent with default attributes
    String message = getPlatformSampleMessage(platform);
    Map<String, String> messageMap = new HashMap<String, String>();
    messageMap.put(platform.name(), message);
    message = SampleMessageGenerator.jsonify(messageMap);
    // For direct publish to mobile end points, topicArn is not relevant.
    publishRequest.setTargetArn(endpointArn);

    // Display the message that will be sent to the endpoint/
    System.out.println("{Message Body: " + message + "}");
    StringBuilder builder = new StringBuilder();
    builder.append("{Message Attributes: ");
    for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes.entrySet()) {
      builder.append(
          "(\"" + entry.getKey() + "\": \"" + entry.getValue().getStringValue() + "\"),");
    }
    builder.deleteCharAt(builder.length() - 1);
    builder.append("}");
    System.out.println(builder.toString());

    publishRequest.setMessage(message);
    return snsClient.publish(publishRequest);
  }
 private CreatePlatformApplicationResult createPlatformApplication(
     String applicationName, Platform platform, String principal, String credential) {
   CreatePlatformApplicationRequest platformApplicationRequest =
       new CreatePlatformApplicationRequest();
   Map<String, String> attributes = new HashMap<String, String>();
   attributes.put("PlatformPrincipal", principal);
   attributes.put("PlatformCredential", credential);
   platformApplicationRequest.setAttributes(attributes);
   platformApplicationRequest.setName(applicationName);
   platformApplicationRequest.setPlatform(platform.name());
   return snsClient.createPlatformApplication(platformApplicationRequest);
 }
 private String getPlatformSampleMessage(Platform platform) {
   switch (platform) {
     case APNS:
       return SampleMessageGenerator.getSampleAppleMessage();
     case APNS_SANDBOX:
       return SampleMessageGenerator.getSampleAppleMessage();
     case GCM:
       return SampleMessageGenerator.getSampleAndroidMessage();
     case ADM:
       return SampleMessageGenerator.getSampleKindleMessage();
     case BAIDU:
       return SampleMessageGenerator.getSampleBaiduMessage();
     case WNS:
       return SampleMessageGenerator.getSampleWNSMessage();
     case MPNS:
       return SampleMessageGenerator.getSampleMPNSMessage();
     default:
       throw new IllegalArgumentException("Platform not supported : " + platform.name());
   }
 }