Amazon SNS GCM/FCM消息有效负载 [英] Amazon SNS GCM/FCM message payload

查看:187
本文介绍了Amazon SNS GCM/FCM消息有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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\" } }"
}

但是当我尝试在Java中实现该功能时,设备不会收到通知.

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;
    }

}

在控制台上响应

发布者请求:{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}

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}

可能是什么问题?

此外,我正在按照SO答案

Also, I am following the pattern suggested in the SO answer here.

推荐答案

终于成功了.我用杰克逊解析器.

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

这篇关于Amazon SNS GCM/FCM消息有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆