Amazon SNS GCM / FCM消息有效负载

时间:2017-03-28 20:56:39

标签: java json amazon-web-services amazon-sns

我正在尝试使用Amazon SNS控制台中的发布终点从我的应用服务器向Android设备发送推送通知(PN),此消息和消息结构为json,它正常工作。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sortable-outer">
    <div class="pc-row sortable">
        <div class="pc-col-xs-4"></div>
        <div class="pc-col-xs-4"></div>
        <div class="pc-col-xs-4"></div>
        <div class="pc-col-xs-4"></div>
    </div>
</div>
<div class="bnr-fpc-fs-controls-control right lg left-arrow-button">
    <svg width="32" height="32" viewBox="0 0 32 32" id="carousel_arrow" x="287" y="384.561">
        <path d="M16 0c8.836 0 16 7.164 16 16s-7.164 16-16 16S0 24.836 0 16 7.164 0 16 0zm0 29c7.168 0 13-5.832 13-13S23.168 3 16 3 3 8.832 3 16s5.832 13 13 13zM13.5 9c.39 0 .743.153 1.01.398l.004-.003 6 5.5a1.5 1.5 0 0 1 0 2.21l-6 5.5-.003-.003c-.267.245-.62.398-1.01.398a1.5 1.5 0 0 1-1.5-1.5c0-.438.19-.828.49-1.102l-.004-.003L17.28 16l-4.794-4.395.003-.003A1.49 1.49 0 0 1 12 10.5 1.5 1.5 0 0 1 13.5 9z"
        fill="#818091" fill-rule="evenodd" />
    </svg>
</div>
<div class="bnr-fpc-fs-controls-control next lg right-arrow-button">
    <svg width="32" height="32" viewBox="0 0 32 32" id="carousel_arrow" x="287" y="384.561">
        <path d="M16 0c8.836 0 16 7.164 16 16s-7.164 16-16 16S0 24.836 0 16 7.164 0 16 0zm0 29c7.168 0 13-5.832 13-13S23.168 3 16 3 3 8.832 3 16s5.832 13 13 13zM13.5 9c.39 0 .743.153 1.01.398l.004-.003 6 5.5a1.5 1.5 0 0 1 0 2.21l-6 5.5-.003-.003c-.267.245-.62.398-1.01.398a1.5 1.5 0 0 1-1.5-1.5c0-.438.19-.828.49-1.102l-.004-.003L17.28 16l-4.794-4.395.003-.003A1.49 1.49 0 0 1 12 10.5 1.5 1.5 0 0 1 13.5 9z"
        fill="#818091" fill-rule="evenodd" />
    </svg>
</div>

但是当我尝试在Java中实现它时,设备不会收到通知。

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

控制台上的响应

  

Publist请求:{TargetArn:   ARN:AWS:SNS:AP-南-1:818862955266:端点/ GCM / TestApp / a1ec8114-58c9-371b-bb76-d8d16e674e52,消息:   { “默认”: “测试”, “GCM”:{ “通知”:{ “文”: “你好”}}},MessageStructure:   json,MessageAttributes:{}} {MessageId:   7dfb613c-06d0-5fe6-8766-3068c9438614}   {AWS_REQUEST_ID = 3d0e13f4-b2be-5c95-ad43-42a07d2d5567}

可能是什么问题?

另外,我遵循SO回答here中建议的模式。

1 个答案:

答案 0 :(得分:0)

这终于奏效了。我用过jackson解析器。

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