问题
I am trying to send a push notification (PN) from my application server to an android device using publish end point in the Amazon SNS console with this message and message structure as json it works fine.
{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}
But when I try to implement the same in Java it the device does not receive the notification.
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;
}
}
Response on the console
Publist request:{TargetArn: arn:aws:sns:ap-south-1:818862955266:endpoint/GCM/TestApp/a1ec8114-58c9-371b-bb76-d8d16e674e52,Message: {"default":"Test","GCM":{"notification":{"text":"hello"}}},MessageStructure: json,MessageAttributes: {}} {MessageId: 7dfb613c-06d0-5fe6-8766-3068c9438614} {AWS_REQUEST_ID=3d0e13f4-b2be-5c95-ad43-42a07d2d5567}
What could be the problem?
Also, I am following the pattern suggested in the SO answer here.
回答1:
This worked finally. I used jackson parser.
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());
来源:https://stackoverflow.com/questions/43079796/amazon-sns-gcm-fcm-message-payload