百度推送java_百度云推送的Java实现

1 packagecom.baidu.yun.sample;2

3 importjava.util.HashMap;4 importjava.util.List;5 importjava.util.Random;6

7 importcom.alibaba.fastjson.JSONObject;8 importcom.baidu.yun.channel.auth.ChannelKeyPair;9 importcom.baidu.yun.channel.client.BaiduChannelClient;10 importcom.baidu.yun.channel.exception.ChannelClientException;11 importcom.baidu.yun.channel.exception.ChannelServerException;12 importcom.baidu.yun.channel.model.PushUnicastMessageRequest;13 importcom.baidu.yun.channel.model.PushUnicastMessageResponse;14 importcom.baidu.yun.core.log.YunLogEvent;15 importcom.baidu.yun.core.log.YunLogHandler;16 importcom.rongyun.io.rong.util.UnLoginUserContent;17 importcom.xiaoma.xmys.interfaces.taskJob.TaskJobConstants;18 importcom.xiaoma.xmys.postcenter.model.LoginTimeBean;19

20 public classIosPushNotificationSample {21

22 public boolean pushMessage(Listlist){23

24 //1. 设置developer平台的ApiKey/SecretKey

25 String apiKey =TaskJobConstants.IOSAPIKEY;26 String secretKey =TaskJobConstants.IOSECRITKEY;27 ChannelKeyPair pair = newChannelKeyPair(apiKey, secretKey);28

29 //2. 创建BaiduChannelClient对象实例

30 BaiduChannelClient channelClient = newBaiduChannelClient(pair);31

32

33 //3. 若要了解交互细节,请注册YunLogHandler类

34 channelClient.setChannelLogHandler(newYunLogHandler() {35 @Override36 public voidonHandle(YunLogEvent event) {37 System.out.println(event.getMessage());38 }39 });40 try{41 for(int i=0;i

43 //4. 创建请求类对象44 //手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的

45 PushUnicastMessageRequest request = newPushUnicastMessageRequest();46 request.setDeviceType(4); //device_type => 1: web 2: pc 3:android47 //4:ios 5:wp

48 request.setDeployStatus(1); //DeployStatus => 1: Developer 2:49 //Production

50 request.setChannelId(Long.parseLong(list.get(i).getChannelId()));51 request.setUserId(String.valueOf(list.get(i).getUserId()));52 request.setMessageType(1);53 JSONObject jsonObject = newJSONObject();54 HashMap< String, Object> map = new HashMap();55

56 String content=UnLoginUserContent.CONTENT1;57 Random rand = newRandom();58 int randNum = rand.nextInt(3);59 if(randNum==0){60 content=UnLoginUserContent.CONTENT2;61 }else if(randNum==1){62 content=UnLoginUserContent.CONTENT3;63 }else{64 content=UnLoginUserContent.CONTENT1;65 }66 map.put("alert", content);67 jsonObject.put("content", "");68 jsonObject.put("aps", map);69 //沉默用户

70 jsonObject.put("type", 1);71

72 request.setMessage(jsonObject.toString());73 //5. 调用pushMessage接口

74 PushUnicastMessageResponse response =channelClient75 .pushUnicastMessage(request);76 //6. 认证推送成功

77 System.out.println("push amount : " +response.getSuccessAmount());78 }79 } catch(ChannelServerException e) {80 //处理服务端错误异常

81 System.out.println(String.format(82 "request_id: %d, error_code: %d, error_message: %s",83 e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));84 } catch(ChannelClientException e) {85 e.printStackTrace();86 }87 return true;88

89 }90

91

92 public booleanpushOnlyMessage(String channelID,String userId){93

94 //1. 设置developer平台的ApiKey/SecretKey

95 String apiKey =TaskJobConstants.IOSAPIKEY;96 String secretKey =TaskJobConstants.IOSECRITKEY;97 ChannelKeyPair pair = newChannelKeyPair(apiKey, secretKey);98

99 //2. 创建BaiduChannelClient对象实例

100 BaiduChannelClient channelClient = newBaiduChannelClient(pair);101

102

103 //3. 若要了解交互细节,请注册YunLogHandler类

104 channelClient.setChannelLogHandler(newYunLogHandler() {105 @Override106 public voidonHandle(YunLogEvent event) {107 System.out.println(event.getMessage());108 }109 });110 try{111

112 //4. 创建请求类对象113 //手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的

114 PushUnicastMessageRequest request = newPushUnicastMessageRequest();115 request.setDeviceType(4); //device_type => 1: web 2: pc 3:android116 //4:ios 5:wp

117 request.setDeployStatus(1); //DeployStatus => 1: Developer 2:118 //Production

119 request.setChannelId(Long.parseLong(channelID));120 request.setUserId(String.valueOf(userId));121 request.setMessageType(1);122 JSONObject jsonObject = newJSONObject();123 HashMap< String, Object> map = new HashMap();124

125 String content=UnLoginUserContent.CONTENT1;126 Random rand = newRandom();127 int randNum = rand.nextInt(3);128 if(randNum==0){129 content=UnLoginUserContent.CONTENT2;130 }else if(randNum==1){131 content=UnLoginUserContent.CONTENT3;132 }else{133 content=UnLoginUserContent.CONTENT1;134 }135 map.put("alert", content);136 jsonObject.put("content", "");137 jsonObject.put("aps", map);138 //沉默用户

139 jsonObject.put("type", 1);140

141 request.setMessage(jsonObject.toString());142 //5. 调用pushMessage接口

143 PushUnicastMessageResponse response =channelClient144 .pushUnicastMessage(request);145 //6. 认证推送成功

146 System.out.println("push amount : " +response.getSuccessAmount());147

148 } catch(ChannelServerException e) {149 //处理服务端错误异常

150 System.out.println(String.format(151 "request_id: %d, error_code: %d, error_message: %s",152 e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));153 } catch(ChannelClientException e) {154 e.printStackTrace();155 }156 return true;157

158 }159

160

161 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值