Amazon SNS GCM/FCM 消息负载
Posted
技术标签:
【中文标题】Amazon SNS GCM/FCM 消息负载【英文标题】:Amazon SNS GCM/FCM message payload 【发布时间】:2017-08-22 03:09:27 【问题描述】:我正在尝试使用 Amazon SNS 控制台中的发布端点将推送通知 (PN) 从我的应用程序服务器发送到 android 设备,此消息和消息结构为 json,它工作正常。
"GCM": " \"notification\": \"text\": \"test message\" "
但是当我尝试在 Java 中实现相同的功能时,设备没有收到通知。
PublishRequest publishRequest = new PublishRequest();
publishRequest.setTargetArn("arn:aws:sns:ap-south-1:818862955266:endpoint/GCM/TestApp/a1ec8114-58c9-371b-bb76-d8d16e674e52");
String message = "\"GCM\": \" \"notification\": \"text\": \"test message\" \"";
ObjectMapper mapper = new ObjectMapper();
PushRequest pushRequest = new PushRequest();
pushRequest.setDef("Test");
GCM gcm = new GCM();
Notification notification = new Notification();
notification.setText("hello");
gcm.setNotification(notification);
pushRequest.setGcm(gcm);
String jsonInString = mapper.writeValueAsString(pushRequest);
publishRequest.setMessage(jsonInString);
publishRequest.setMessageStructure("json");
System.out.println("Publist request:"+publishRequest.toString());
PublishResult publishResult = amazonSNSTemplate.getAmazonSNSClient().publish(publishRequest);
System.out.println(publishResult.toString());
System.out.println(publishResult.getSdkResponseMetadata().toString());
public class PushRequest
@JsonProperty("default")
private String def;
@JsonProperty("GCM")
private GCM gcm;
public String getDef()
return def;
public void setDef(String def)
this.def = def;
public GCM getGcm()
return gcm;
public void setGcm(GCM gcm)
this.gcm = gcm;
public class GCM
private Notification notification;
@JsonProperty("notification")
public Notification getNotification()
return notification;
public void setNotification(Notification notification)
this.notification = notification;
public class Notification
private String text;
@JsonProperty("text")
public String getText()
return text;
public void setText(String text)
this.text = text;
控制台响应
发布请求:TargetArn: arn:aws:sns:ap-south-1:818862955266:endpoint/GCM/TestApp/a1ec8114-58c9-371b-bb76-d8d16e674e52,消息: "default":"Test","GCM":"notification":"text":"hello",MessageStructure: json,MessageAttributes: MessageId: 7dfb613c-06d0-5fe6-8766-3068c9438614 AWS_REQUEST_ID=3d0e13f4-b2be-5c95-ad43-42a07d2d5567
可能是什么问题?
另外,我遵循 SO 答案 here 中建议的模式。
【问题讨论】:
【参考方案1】:这终于奏效了。我用的是杰克逊解析器。
public class PushRequest
@JsonProperty("default")
private String def;
@JsonProperty("GCM")
private GCM gcm;
public String getDef()
return def;
public void setDef(String def)
this.def = def;
public GCM getGcm()
return gcm;
public void setGcm(GCM gcm)
this.gcm = gcm;
public class GCM
private Notification notification;
@JsonProperty("notification")
public Notification getNotification()
return notification;
public void setNotification(Notification notification)
this.notification = notification;
public class Notification
private String text;
@JsonProperty("text")
public String getText()
return text;
public void setText(String text)
this.text = text;
PublishRequest publishRequest = new PublishRequest();
publishRequest.setTargetArn("arn:aws:sns:ap-south-1:818862955266:endpoint/GCM/TestApp/ac338195-1b87-3521-bd98-b7867a83ff27");
// String message = "\"GCM\": \" \"notification\": \"text\": \"test message\" \"";
ObjectMapper mapper = new ObjectMapper();
PushRequest pushRequest = new PushRequest();
pushRequest.setDef("Testing out FB messages");
GCM gcm = new GCM();
Notification notification = new Notification();
notification.setText("hello");
gcm.setNotification(notification);
pushRequest.setGcm(gcm);
String jsonInString = mapper.writeValueAsString(pushRequest);
publishRequest.setMessage(jsonInString);
publishRequest.setMessageStructure("json");
System.out.println("Publist request:"+publishRequest.toString());
PublishResult publishResult = amazonSNSTemplate.getAmazonSNSClient().publish(publishRequest);
System.out.println(publishResult.toString());
System.out.println(publishResult.getSdkResponseMetadata().toString());
【讨论】:
以上是关于Amazon SNS GCM/FCM 消息负载的主要内容,如果未能解决你的问题,请参考以下文章
推送通知的 Amazon SNS android 设备令牌限制
用于移动推送通知的 AWS SNS 的最大有效负载长度是多少?