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 CreatePlatformEndpointResult createPlatformEndpoint(
Platform platform, String customData, String platformToken,
String applicationArn) {
CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest();
platformEndpointRequest.setCustomUserData(customData);
String token = platformToken;
String userId = null;
if (platform == Platform.BAIDU) {
String[] tokenBits = platformToken.split("\\|");
token = tokenBits[0];
userId = tokenBits[1];
Map<String, String> endpointAttributes = new HashMap<String, String>();
endpointAttributes.put("UserId", userId);
endpointAttributes.put("ChannelId", token);
platformEndpointRequest.setAttributes(endpointAttributes);
}
platformEndpointRequest.setToken(token);
platformEndpointRequest.setPlatformApplicationArn(applicationArn);
return snsClient.createPlatformEndpoint(platformEndpointRequest);
}
private void deletePlatformApplication(String applicationArn) {
DeletePlatformApplicationRequest request = new DeletePlatformApplicationRequest();
request.setPlatformApplicationArn(applicationArn);
snsClient.deletePlatformApplication(request);
}
private PublishResult publish(String endpointArn, Platform platform,
Map<Platform, Map<String, MessageAttributeValue>> attributesMap,String messageToSend) {
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,messageToSend);
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);
}
public void demoNotification(Platform platform, String principal,
String credential, String platformToken, String applicationName,
Map<Platform, Map<String, MessageAttributeValue>> attrsMap,String message) {
// Create Platform Application. This corresponds to an app on a
// platform.
CreatePlatformApplicationResult platformApplicationResult = createPlatformApplication(
applicationName, platform, principal, credential);
System.out.println(platformApplicationResult);
// The Platform Application Arn can be used to uniquely identify the
// Platform Application.
String platformApplicationArn = platformApplicationResult
.getPlatformApplicationArn();
// Create an Endpoint. This corresponds to an app on a device.
CreatePlatformEndpointResult platformEndpointResult = createPlatformEndpoint(
platform,
"CustomData - Useful to store endpoint specific data",
platformToken, platformApplicationArn);
System.out.println(platformEndpointResult);
// Publish a push notification to an Endpoint.
PublishResult publishResult = publish(
platformEndpointResult.getEndpointArn(), platform, attrsMap,message);
System.out.println("Published! \n{MessageId="
+ publishResult.getMessageId() + "}");
// Delete the Platform Application since we will no longer be using it.
deletePlatformApplication(platformApplicationArn);
}
private String getPlatformSampleMessage(Platform platform,String messageToSend) {
switch (platform) {
case APNS:
return SampleMessageGenerator.getSampleAppleMessage();
case APNS_SANDBOX:
return SampleMessageGenerator.getSampleAppleMessage();
case GCM:
return SampleMessageGenerator.getSampleAndroidMessage(messageToSend);
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());
}
}
public static Map<String, MessageAttributeValue> getValidNotificationAttributes(
Map<String, MessageAttributeValue> notificationAttributes) {
Map<String, MessageAttributeValue> validAttributes = new HashMap<String,
MessageAttributeValue>();
if (notificationAttributes == null) return validAttributes;
for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes
.entrySet()) {
if (!StringUtils.isBlank(entry.getValue().getStringValue())) {
validAttributes.put(entry.getKey(), entry.getValue());
}
}
return validAttributes;
}
}
public class AmazonSNSClientWrapper {
private final AmazonSNS snsClient;
public AmazonSNSClientWrapper(AmazonSNS client) {
this.snsClient = client;
}
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 CreatePlatformEndpointResult createPlatformEndpoint(
Platform platform, String customData, String platformToken,
String applicationArn) {
CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest();
platformEndpointRequest.setCustomUserData(customData);
String token = platformToken;
String userId = null;
if (platform == Platform.BAIDU) {
String[] tokenBits = platformToken.split("\\|");
token = tokenBits[0];
userId = tokenBits[1];
Map<String, String> endpointAttributes = new HashMap<String, String>();
endpointAttributes.put("UserId", userId);
endpointAttributes.put("ChannelId", token);
platformEndpointRequest.setAttributes(endpointAttributes);
}
platformEndpointRequest.setToken(token);
platformEndpointRequest.setPlatformApplicationArn(applicationArn);
return snsClient.createPlatformEndpoint(platformEndpointRequest);
}
private void deletePlatformApplication(String applicationArn) {
DeletePlatformApplicationRequest request = new DeletePlatformApplicationRequest();
request.setPlatformApplicationArn(applicationArn);
snsClient.deletePlatformApplication(request);
}
private PublishResult publish(String endpointArn, Platform platform,
Map<Platform, Map<String, MessageAttributeValue>> attributesMap, String msgText) {
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 = getPlatformMessage(platform, msgText);
Map<String, String> messageMap = new HashMap<String, String>();
messageMap.put(platform.name(), message);
message = MessageGenerator.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);
}
public void NotificationWorker(Platform platform, String principal,
String credential, String platformToken, String applicationName,
Map<Platform, Map<String, MessageAttributeValue>> attrsMap, String message) {
// Create Platform Application. This corresponds to an app on a
// platform.
CreatePlatformApplicationResult platformApplicationResult = createPlatformApplication(
applicationName, platform, principal, credential);
System.out.println(platformApplicationResult);
// The Platform Application Arn can be used to uniquely identify the
// Platform Application.
String platformApplicationArn = platformApplicationResult
.getPlatformApplicationArn();
// Create an Endpoint. This corresponds to an app on a device.
CreatePlatformEndpointResult platformEndpointResult = createPlatformEndpoint(
platform,
"CustomData - Useful to store endpoint specific data",
platformToken, platformApplicationArn);
System.out.println(platformEndpointResult);
// Publish a push notification to an Endpoint.
PublishResult publishResult = publish(
platformEndpointResult.getEndpointArn(), platform, attrsMap,message);
System.out.println("Published! \n{MessageId="
+ publishResult.getMessageId() + "}");
// Delete the Platform Application since we will no longer be using it.
deletePlatformApplication(platformApplicationArn);
}
private String getPlatformMessage(Platform platform) {
switch (platform) {
case APNS:
return MessageGenerator.sendAppleMessage();
case APNS_SANDBOX:
return MessageGenerator.sendAppleMessage();
case GCM:
return MessageGenerator.getAndroidMessage();
case ADM:
return MessageGenerator.getKindleMessage();
case WNS:
return MessageGenerator.getWNSMessage();
case MPNS:
return MessageGenerator.getMPNSMessage();
default:
throw new IllegalArgumentException("Platform not supported : "
+ platform.name());
}
}
public static Map<String, MessageAttributeValue> getValidNotificationAttributes(
Map<String, MessageAttributeValue> notificationAttributes) {
if (notificationAttributes != null) {
Map<String, MessageAttributeValue> validAttributes = new HashMap<String, MessageAttributeValue>();
for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes.entrySet()) {
if (!StringUtils.isBlank(entry.getValue().getStringValue())) {
validAttributes.put(entry.getKey(), entry.getValue());
}
}
return validAttributes;
} else {
return new HashMap<String, MessageAttributeValue>();
}
}
}
Я изменил код в соответствии с моими требованиями, но он будет работать. Просто проверьте некоторые имена методов, потому что я их изменил. Надеюсь, поможет
snsClient
нет метода с этой сигнатурой, но не видя соответствующего кода, я могу только догадываться.