简体   繁体   English

Amazon SNS GCM / FCM消息有效负载

[英]Amazon SNS GCM/FCM message payload

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. 我正在尝试使用Amazon SNS控制台中的发布端点将来自我的应用程序服务器的推送通知(PN)发送到android设备,此消息和消息结构为json,效果很好。

{
"GCM": "{ \"notification\": { \"text\": \"test message\" } }"
}

But when I try to implement the same in Java it the device does not receive the notification. 但是,当我尝试在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;
    }

}

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} 发布清单要求:{TargetArn:arn:aws:sns:ap-south-1:818862955266:endpoint / GCM / TestApp / a1ec8114-58c9-371b-bb76-d8d16e674e52,消息:{“默认”:“测试”,“ 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 . 另外,我遵循此处 SO答案中建议的模式。

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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM