百度云推送java端报错parameter msg must be Json-Array-String解决方法


百度云推送javasdk代码:

文件1:Baidu-Push-Server-SDK-Java-3.0.1\javasdk_3.0.1\sample\com\baidu\yun\push\sample\AndroidPushMsgToAll.java

代码片段:

try {
// 4. specify request arguments
PushMsgToAllRequest request = new PushMsgToAllRequest()
.addMsgExpires(new Integer(3600)).addMessageType(0)
.addMessage("Hello Baidu push") //添加透传消息
.addSendTime(System.currentTimeMillis() / 1000 + 120) // 设置定时推送时间,必需超过当前时间一分钟,单位秒.实例2分钟后推送
.addDeviceType(3);
// 5. http request
PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
// Http请求结果解析打印
System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
+ response.getSendTime() + ",timerId: "
+ response.getTimerId());
} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
throw e;
} else {
e.printStackTrace();
}

文件2:Baidu-Push-Server-SDK-Java-3.0.1\javasdk_3.0.1\sample\com\baidu\yun\push\sample\AndroidPushMsgToTag.java

代码片段:

try {
// 4. specify request arguments
// pushTagTpye = 1 for common tag pushing
PushMsgToTagRequest request = new PushMsgToTagRequest()
.addTagName("xxxxx")
.addMsgExpires(new Integer(3600))
.addMessageType(0)  // 添加透传消息
// .addSendTime(System.currentTimeMillis() / 1000 + 120) //设置定时任务
.addMessage("Hello Baidu push")
.addDeviceType(3);
// 5. http request
PushMsgToTagResponse response = pushClient.pushMsgToTag(request);
// Http请求结果解析打印
System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
+ response.getSendTime() + ",timerId: "
+ response.getTimerId());
} catch (PushClientException e) {
if (BaiduPushConstants.ERROROPTTYPE) {
throw e;
} else {
e.printStackTrace();
}


以上红色部分为报错原因

具体解决办法:

将.addMessage("Hello Baidu push") 替换为 .addMessage("{\"title\":\"TEST\",\"description\":\"Hello Baidu push!\")。

也就说addMessage的参数为json.toString();。这里sdk里面存在错误。


下面为正确的文件代码片段:

文件3:Baidu-Push-Server-SDK-Java-3.0.1\javasdk_3.0.1\sample\com\baidu\yun\push\sample\AndroidPushMsgToSingleDevice.java

代码片段:

try {
// 4. specify request arguments
//创建 Android的通知
JSONObject notification = new JSONObject();
notification.put("title", "TEST");
notification.put("description","Hello Baidu Push");
notification.put("notification_builder_id", 0);
notification.put("notification_basic_style", 4);
notification.put("open_type", 1);
notification.put("url", "http://push.baidu.com");
JSONObject jsonCustormCont = new JSONObject();
jsonCustormCont.put("key", "value"); //自定义内容,key-value
notification.put("custom_content", jsonCustormCont);


PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
.addChannelId("xxxxxxxxxxxxxxxxxxxxx")
.addMsgExpires(new Integer(3600)). // message有效时间
addMessageType(1).// 1:通知,0:透传消息. 默认为0 注:IOS只有通知.
addMessage(notification.toString()).
addDeviceType(3);// deviceType => 3:android, 4:ios
// 5. http request
PushMsgToSingleDeviceResponse response = pushClient
.pushMsgToSingleDevice(request);
// Http请求结果解析打印
System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
+ response.getSendTime());
} catch (PushClientException e) {
/*
* ERROROPTTYPE 用于设置异常的处理方式 -- 抛出异常和捕获异常,'true' 表示抛出, 'false' 表示捕获。
*/
if (BaiduPushConstants.ERROROPTTYPE) {
throw e;
} else {
e.printStackTrace();
}


message具体格式请参考百度api参数说明:http://push.baidu.com/doc/restapi/msg_struct

百度云推送java端sdk3.0 http://download.csdn.net/detail/xingjianwu/9187131 

百度云推送安卓端sdk4.5  http://download.csdn.net/detail/xingjianwu/9187119

百度云推送检测工具  :http://download.csdn.net/detail/xingjianwu/9187081




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值