资源描述
package net;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
public class TestPushMessage {
/**
* 测试推送
*/
public static void main(String[] args) {
String string = "{\"title\":\"你有新消息\",\"description\":\"今天下午全体员工发奖金\","
+ "\"notification_builder_id\":0,\"notification_basic_style\":2,\"custom_content\": {\"id\":\"485446\"},\"open_type\":\"2\"}";
//String string ="hello";
//int status = pushBroadcastMessage(string,3,1);
int status = pushOnlyPleploMessage(string,3,1);
System.out.println(status);
}
/**
* 初始化百度推送
*/
private static BaiduChannelClient initPushClient()
{
//这两个key自己申请
String apiKey = "自己申请的apikey";
String secretKey = "自己申请的secretKey";
// 1. 设置developer平台的ApiKey/SecretKey
ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);
// 2. 创建BaiduChannelClient对象实例
BaiduChannelClient channelClient = new BaiduChannelClient(pair);
// 3. 若要了解交互细节,请注册YunLogHandler类
channelClient.setChannelLogHandler(new YunLogHandler()
{
@Override
public void onHandle(YunLogEvent event)
{
System.out.println(event.getMessage());
}
});
return channelClient;
}
/**
* 用百度推送向所有人发送消息或者通知
*/
public static int pushBroadcastMessage(String Content,int devType,int pushType)
{
BaiduChannelClient channelClient = initPushClient();
try{
// 4. 创建请求类对象
PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
// devType => 1: web 2: pc 3:android 4:ios 5:wp
request.setDeviceType(devType);
// pushType为0设置发送消息,为 1设置发送通知
request.setMessageType(pushType);
request.setMessage(Content);
// 5. 调用pushMessage接口
PushBroadcastMessageResponse response = channelClient
.pushBroadcastMessage(request);
// 6. 认证推送成功
System.out.println("认证推送成功push amount : " + response.getSuccessAmount());
return response.getSuccessAmount();
}
catch (ChannelClientException e)
{
// 处理客户端错误异常
e.printStackTrace();
return 2;
}
catch (ChannelServerException e)
{
// 处理服务端错误异常
System.out.println(String.format(
"request_id: %d, error_code: %d, error_message: %s",
e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
return 3;
}
}
/**
* 向单个人推送消息或者通知
* @return
*/
public static int pushOnlyPleploMessage(String Content,int devType,int pushType){
BaiduChannelClient channelClient = initPushClient();
try {
// 4. 创建请求类对象
// 手机端的ChannelId, 手机端的UserId, 用户需替换为自己的
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
// devType => 1: web 2: pc 3:android 4:ios 5:wp
request.setDeviceType(devType);
//ChannelId、UserId这两个为每个手机绑定的,根据需要自己填写,暂用11111111111111代替
request.setChannelId(111111111111L);
request.setUserId("111111111111111111");
// request.setMessageType(pushType); //pushType为0时发送消息
// request.setMessage("Hello Channel");
request.setMessageType(pushType);// pushType为1时发送通知
request.setMessage(Content);
// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);
// 6. 认证推送成功
System.out.println("认证推送成功push amount : " + response.getSuccessAmount());
return response.getSuccessAmount();
} catch (ChannelClientException e) {
// 处理客户端错误异常
e.printStackTrace();
return 2;
} catch (ChannelServerException e) {
// 处理服务端错误异常
System.out.println(String.format(
"request_id: %d, error_code: %d, error_message: %s",
e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
return 3;
}
}
}
展开阅读全文