80,359
社区成员
发帖
与我相关
我的任务
分享
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编码过的字节数组字符串
}
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();
}
}