百度云推送的Java实现
推送现在基本APP都有,项目中要通知和消息,所以综合考虑用了百度云推送
Java实现步骤:
1、 下载 http://push.baidu.com/sdk/push_server_sdk_for_java;
2、把 代码考到自己的项目。我新建了一个目录;
3、服务端代码实现, 这个DEMo基本都实现了所有代码,我们所做的就是 把 我们的APPkey 和appsecret 换掉,改写下demo中的方法,为我们使用,
之前写了一段 错误代码:
1 package com.baidu.yun.sample; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Random; 6 7 import com.alibaba.fastjson.JSONObject; 8 import com.baidu.yun.channel.auth.ChannelKeyPair; 9 import com.baidu.yun.channel.client.BaiduChannelClient; 10 import com.baidu.yun.channel.exception.ChannelClientException; 11 import com.baidu.yun.channel.exception.ChannelServerException; 12 import com.baidu.yun.channel.model.PushUnicastMessageRequest; 13 import com.baidu.yun.channel.model.PushUnicastMessageResponse; 14 import com.baidu.yun.core.log.YunLogEvent; 15 import com.baidu.yun.core.log.YunLogHandler; 16 import com.rongyun.io.rong.util.UnLoginUserContent; 17 import com.xiaoma.xmys.interfaces.taskJob.TaskJobConstants; 18 import com.xiaoma.xmys.postcenter.model.LoginTimeBean; 19 20 public class IosPushNotificationSample { 21 22 public boolean pushMessage(List<LoginTimeBean> list){ 23 24 // 1. 设置developer平台的ApiKey/SecretKey 25 String apiKey = TaskJobConstants.IOSAPIKEY; 26 String secretKey = TaskJobConstants.IOSECRITKEY; 27 ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey); 28 29 // 2. 创建BaiduChannelClient对象实例 30 BaiduChannelClient channelClient = new BaiduChannelClient(pair); 31 32 33 // 3. 若要了解交互细节,请注册YunLogHandler类 34 channelClient.setChannelLogHandler(new YunLogHandler() { 35 @Override 36 public void onHandle(YunLogEvent event) { 37 System.out.println(event.getMessage()); 38 } 39 }); 40 try { 41 for(int i=0;i<list.size();i++){ 42 43 // 4. 创建请求类对象 44 // 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的 45 PushUnicastMessageRequest request = new PushUnicastMessageRequest(); 46 request.setDeviceType(4); // device_type => 1: web 2: pc 3:android 47 // 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 = new JSONObject(); 54 HashMap< String, Object> map = new HashMap<String, Object>(); 55 56 String content=UnLoginUserContent.CONTENT1; 57 Random rand = new Random(); 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 = channelClient 75 .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 boolean pushOnlyMessage(String channelID,String userId){ 93 94 // 1. 设置developer平台的ApiKey/SecretKey 95 String apiKey = TaskJobConstants.IOSAPIKEY; 96 String secretKey = TaskJobConstants.IOSECRITKEY; 97 ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey); 98 99 // 2. 创建BaiduChannelClient对象实例 100 BaiduChannelClient channelClient = new BaiduChannelClient(pair); 101 102 103 // 3. 若要了解交互细节,请注册YunLogHandler类 104 channelClient.setChannelLogHandler(new YunLogHandler() { 105 @Override 106 public void onHandle(YunLogEvent event) { 107 System.out.println(event.getMessage()); 108 } 109 }); 110 try { 111 112 // 4. 创建请求类对象 113 // 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的 114 PushUnicastMessageRequest request = new PushUnicastMessageRequest(); 115 request.setDeviceType(4); // device_type => 1: web 2: pc 3:android 116 // 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 = new JSONObject(); 123 HashMap< String, Object> map = new HashMap<String, Object>(); 124 125 String content=UnLoginUserContent.CONTENT1; 126 Random rand = new Random(); 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 = channelClient 144 .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 }
这里是单播 ,把 需要发送的channelID和userID 的集合传过来,结果因为有的channelID和userID 有时候 不对,所以改用pushOnlyMessage的方法;