我想通过SNS定期发送静音。这是由Cloudwatch事件触发的。 推送消息熄灭,但它以原始文本形式发送,并且不会被SNS转换为静默推送格式。正如您所看到的,SNS主题发布工作正常,并且无声推送出去了。 我究竟做错了什么?
答案 0 :(得分:0)
不要挣扎于此,将其发送到lambda函数,您可以在其中创建JSON请求。
public String handle(JsonObject request, Context lambdaContext) throws
BadRequestException, InternalErrorException {
logger = lambdaContext.getLogger();
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
AmazonSNSClient client = new AmazonSNSClient().withRegion(Regions.EU_WEST_1);
PublishRequest rq = new PublishRequest();
rq.setTopicArn(ENDPOINT_TOPIC);
rq.setMessageStructure("json");
rq.setMessage(getSilentAppleMessage());
messageAttributes.put("AWS.SNS.MOBILE.APNS.TTL", new MessageAttributeValue().withDataType("String").withStringValue("120"));
rq.setMessageAttributes(messageAttributes);
client.publish(rq);
GeneralResponse output = new GeneralResponse();
output.setSuccess(true);
return getGson().toJson(output, GeneralResponse.class);
}
public static String getSilentAppleMessage() {
Map<String, Object> mainMessageMap = new HashMap<String, Object>();
Map<String, Object> appleMessageMap = new HashMap<String, Object>();
Map<String, Object> appMessageMap = new HashMap<String, Object>();
appMessageMap.put("content-available", 1);
appleMessageMap.put("aps", appMessageMap);
mainMessageMap.put("default", "");
mainMessageMap.put("APNS_SANDBOX", jsonify(appleMessageMap));
logger.log(jsonify(mainMessageMap));
return jsonify(mainMessageMap);
}
private static String jsonify(Object message) {
try {
return objectMapper.writeValueAsString(message);
} catch (Exception e) {
e.printStackTrace();
throw (RuntimeException) e;
}
}