如何使用百度云 API 推送富媒体消息,百度推送的富媒体发送官方接口是什么?

面向未来_ 2014-08-08 05:38:29
在api文档中看到

http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api/list#push_msg

指定消息内容,单个消息为单独字符串。如果有二进制的消息内容,请先做 BASE64 的编码。
当message_type为1 (通知类型),请按以下格式指定消息内容。
通知消息格式及默认值:


请问一下如何做Base64编码? 我这样做对吗?

private static String getMediaString(){
File file = new File("pic/pic_00.jpg");
try {
String str = file2String(file);
return str ;
} catch (Exception e) {
e.printStackTrace();
}

return "";
}

/**
* 对文件进行编码
* @param file 需要编码的问家
* @return 对文件进行base64编码后的字符串
* @throws Exception
*/
public static String file2String(File file) throws Exception{
FileInputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(file);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//将buffer转化为string
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串

}

在使用api推送时代码如下

public static void pushAndroidNotification(){

ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

BaiduChannelClient channelClient = new BaiduChannelClient(pair);
channelClient.setChannelLogHandler(new YunLogHandler() {

@Override
public void onHandle(YunLogEvent event) {
System.out.println(event.getMessage());
}
});

try {
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(3);
request.setUserId("106085369838333763");

request.setMessageType(1);
//request.setMessage("{\"title\":\"Notify_title_danbo\",\"description\":\"aaa\"}");
request.setMessage("{\"title\":\"Notify_title_danbo\",\"description\":\""+getMediaString()+"\"}");

// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);

// 6. 认证推送成功
System.out.println("push amount : " + response.getSuccessAmount());
} catch (Exception e) {
e.printStackTrace();
}
}


执行的时候如下异常
HttpStatusCode:[400]
Response:[{"request_id":3107758583,"error_code":30602,"error_msg":"request params not valid. parameter messages not valid"}]


com.baidu.yun.channel.exception.ChannelServerException
at com.baidu.yun.channel.transform.ChannelRestResponseJsonUnmapper.unmarshall(Unknown Source)
at com.baidu.yun.channel.client.BaiduChannelClient.pushTagMessage(Unknown Source)
at com.test.main.PushMessage.pushAndroidMessage(PushMessage.java:103)
at com.test.main.PushMessage.main(PushMessage.java:306)

请问谁有遇到过这样的问题? 实际我就是想通过代码推送图片+文件
...全文
320 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
面向未来_ 2014-08-08
  • 打赏
  • 举报
回复
刚查了一下百度的FAQ,现在还不支持REST API推送

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧