jiapeng 7 years ago
parent
commit
37a9456892

+ 63 - 0
src/main/java/com/ekexiu/operation/push/getui/Message.java

@ -0,0 +1,63 @@
1
package com.ekexiu.operation.push.getui;
2

3
import com.google.gson.annotations.SerializedName;
4

5
public class Message {
6
	//注册应用时生成的appkey 
7
	private String appKey;
8
	//是否离线推送 
9
	@SerializedName("is_offine")
10
	private boolean isOffline = true;
11
	//消息应用类型,可选项:notification、link、notypopload、transmission
12
	private String msgtype;
13
	//消息离线存储有效期,单位:ms
14
	@SerializedName("offline_expire_time")
15
	private int offlineExpireTime = 1000*60*60*24;
16
	//选择推送消息使用网络类型,0:不限制,1:wifi
17
	
18
	@SerializedName("push_network_type")
19
	private int pushNetworkType = 0;
20

21
	public String getAppKey() {
22
		return appKey;
23
	}
24

25
	public void setAppKey(String appKey) {
26
		this.appKey = appKey;
27
	}
28

29
	public boolean isOffline() {
30
		return isOffline;
31
	}
32

33
	public void setOffline(boolean isOffline) {
34
		this.isOffline = isOffline;
35
	}
36

37
	public String getMsgtype() {
38
		return msgtype;
39
	}
40

41
	public void setMsgtype(String msgtype) {
42
		this.msgtype = msgtype;
43
	}
44

45
	public int getOfflineExpireTime() {
46
		return offlineExpireTime;
47
	}
48

49
	public void setOfflineExpireTime(int offlineExpireTime) {
50
		this.offlineExpireTime = offlineExpireTime;
51
	}
52

53
	public int getPushNetworkType() {
54
		return pushNetworkType;
55
	}
56

57
	public void setPushNetworkType(int pushNetworkType) {
58
		this.pushNetworkType = pushNetworkType==0?0:1;
59
	}
60
	
61
	
62
	
63
}

+ 73 - 0
src/main/java/com/ekexiu/operation/push/getui/MsgStyle.java

@ -0,0 +1,73 @@
1
package com.ekexiu.operation.push.getui;
2

3
import com.google.gson.annotations.SerializedName;
4

5
public class MsgStyle {
6
	//type 	integer 	是 	固定为1
7
	private int type=1;
8
	//text 	String 	是 	通知内容
9
	private String text;
10
	//title 	String 	是 	通知标题
11
	private String title;
12
	//logo 	String 	是 	通知的图标名称,包含后缀名(需要在客户端开发时嵌入),如“push.png”
13
	private String logo;
14
	//logourl 	String 	否 	通知图标URL地址
15
	private String logourl;
16
	//is_ring 	boolean 	否 	收到通知是否响铃:true响铃,false不响铃。默认响铃
17
	@SerializedName("is_ring")
18
	private boolean ring=true;
19
	//is_vibrate 	boolean 	否 	收到通知是否振动:true振动,false不振动。默认振动
20
	@SerializedName("is_vibrate")
21
	private boolean vibrate = true;
22
	//is_clearable 	boolean 	否 	通知是否可清除: true可清除,false不可清除。默认可清除	
23
	@SerializedName("is_clearable")
24
	private boolean clearable;
25
	public int getType() {
26
		return type;
27
	}
28
	public void setType(int type) {
29
		this.type = type;
30
	}
31
	public String getText() {
32
		return text;
33
	}
34
	public void setText(String text) {
35
		this.text = text;
36
	}
37
	public String getTitle() {
38
		return title;
39
	}
40
	public void setTitle(String title) {
41
		this.title = title;
42
	}
43
	public String getLogo() {
44
		return logo;
45
	}
46
	public void setLogo(String logo) {
47
		this.logo = logo;
48
	}
49
	public String getLogourl() {
50
		return logourl;
51
	}
52
	public void setLogourl(String logourl) {
53
		this.logourl = logourl;
54
	}
55
	public boolean isRing() {
56
		return ring;
57
	}
58
	public void setRing(boolean ring) {
59
		this.ring = ring;
60
	}
61
	public boolean isVibrate() {
62
		return vibrate;
63
	}
64
	public void setVibrate(boolean vibrate) {
65
		this.vibrate = vibrate;
66
	}
67
	public boolean isClearable() {
68
		return clearable;
69
	}
70
	public void setClearable(boolean clearable) {
71
		this.clearable = clearable;
72
	}
73
}

+ 53 - 0
src/main/java/com/ekexiu/operation/push/getui/NotificationMessage.java

@ -0,0 +1,53 @@
1
package com.ekexiu.operation.push.getui;
2

3
import com.google.gson.annotations.SerializedName;
4

5
public class NotificationMessage {
6
	//transmission_type 	boolean 	否 	收到消息是否立即启动应用,true为立即启动,false则广播等待启动,默认是否
7
	@SerializedName("transmission_type")
8
	private boolean transmissionType=false;
9
	
10
	//transmission_content 	String 	否 	透传内容
11
	@SerializedName("transmission_content")
12
	private String transmissionContent;
13
	
14
	//duration_begin 	String 	否 	设定展示开始时间,格式为yyyy-MM-dd HH:mm:ss
15
	@SerializedName("duration_begin")
16
	private String durationBegin;
17
	
18
	//duration_end 	String 	否 	设定展示结束时间,格式为yyyy-MM-dd HH:mm:ss
19
	@SerializedName("duration_end")
20
	private String durationEnd;
21
	//style 	Style 	是 	通知栏消息布局样式,见底下Style说明
22
	private MsgStyle style;
23
	public boolean isTransmissionType() {
24
		return transmissionType;
25
	}
26
	public void setTransmissionType(boolean transmissionType) {
27
		this.transmissionType = transmissionType;
28
	}
29
	public String getTransmissionContent() {
30
		return transmissionContent;
31
	}
32
	public void setTransmissionContent(String transmissionContent) {
33
		this.transmissionContent = transmissionContent;
34
	}
35
	public String getDurationBegin() {
36
		return durationBegin;
37
	}
38
	public void setDurationBegin(String durationBegin) {
39
		this.durationBegin = durationBegin;
40
	}
41
	public String getDurationEnd() {
42
		return durationEnd;
43
	}
44
	public void setDurationEnd(String durationEnd) {
45
		this.durationEnd = durationEnd;
46
	}
47
	public MsgStyle getStyle() {
48
		return style;
49
	}
50
	public void setStyle(MsgStyle style) {
51
		this.style = style;
52
	}
53
}

+ 16 - 0
src/main/java/com/ekexiu/operation/push/getui/PushMessage.java

@ -0,0 +1,16 @@
1
package com.ekexiu.operation.push.getui;
2

3
public class PushMessage {
4
	private final Message message;
5
	
6
	private NotificationMessage notification;
7
	private TransmissionMessage transmission;
8
	
9
	private String requestid;
10
	private String alias;
11
	
12
	public PushMessage(){
13
		this.message = new Message();
14
	}
15
	
16
}

+ 57 - 0
src/main/java/com/ekexiu/operation/push/getui/TransmissionMessage.java

@ -0,0 +1,57 @@
1
package com.ekexiu.operation.push.getui;
2

3
import java.util.Map;
4

5
import com.google.gson.annotations.SerializedName;
6

7
public class TransmissionMessage {
8
	//transmission_type 	boolean 	否 	收到消息是否立即启动应用,true为立即启动,false则广播等待启动,默认是否
9
	@SerializedName("transmission_type")
10
	private boolean transmissionType=false;
11
	
12
	//transmission_content 	String 	否 	透传内容
13
	@SerializedName("transmission_content")
14
	private String transmissionContent;
15
	
16
	//duration_begin 	String 	否 	设定展示开始时间,格式为yyyy-MM-dd HH:mm:ss
17
	@SerializedName("duration_begin")
18
	private String durationBegin;
19
	
20
	//duration_end 	String 	否 	设定展示结束时间,格式为yyyy-MM-dd HH:mm:ss
21
	@SerializedName("duration_end")
22
	private String durationEnd;
23
	//push_info 	Map 	否 	APNs消息内容
24
	@SerializedName("push_info")
25
	private Map<String,Object> pushInfo;
26
	public boolean isTransmissionType() {
27
		return transmissionType;
28
	}
29
	public void setTransmissionType(boolean transmissionType) {
30
		this.transmissionType = transmissionType;
31
	}
32
	public String getTransmissionContent() {
33
		return transmissionContent;
34
	}
35
	public void setTransmissionContent(String transmissionContent) {
36
		this.transmissionContent = transmissionContent;
37
	}
38
	public String getDurationBegin() {
39
		return durationBegin;
40
	}
41
	public void setDurationBegin(String durationBegin) {
42
		this.durationBegin = durationBegin;
43
	}
44
	public String getDurationEnd() {
45
		return durationEnd;
46
	}
47
	public void setDurationEnd(String durationEnd) {
48
		this.durationEnd = durationEnd;
49
	}
50
	public Map<String, Object> getPushInfo() {
51
		return pushInfo;
52
	}
53
	public void setPushInfo(Map<String, Object> pushInfo) {
54
		this.pushInfo = pushInfo;
55
	}
56
	
57
}

+ 12 - 1
src/main/java/com/ekexiu/portal/dao/ProfessorDao.java

@ -280,7 +280,7 @@ public abstract class ProfessorDao {
280 280
	public EditProfessor queryBaseInfo(Connection con, String id) throws SQLException {
281 281
		int index = 1;
282 282
        String sql = "SELECT P.OFFICE,P.DEPARTMENT,P.TITLE,AUTHENTICATION,AUTH_TYPE,P.AUTH_STATUS,"
283
        		+ " P.ID,P.NAME,P.ADDRESS,P.ORG_AUTH,ORGANIZATION.NAME,P.ORG_ID,P.SCORE_PERCENT,P.SHARE_ID "
283
        		+ " P.ID,P.NAME,P.ADDRESS,P.ORG_AUTH,ORGANIZATION.NAME,P.ORG_ID,P.SCORE_PERCENT,P.SHARE_ID,P.PHONE,P.EMAIL"
284 284
        		+ " FROM PROFESSOR P LEFT JOIN ORGANIZATION ON P.ORG_ID = ORGANIZATION.ID WHERE P.ID = ?";
285 285
        PreparedStatement ps = con.prepareStatement(sql);
286 286
        try{
@ -335,6 +335,17 @@ public abstract class ProfessorDao {
335 335
                    }
336 336
                    professor.setScorePercent(scorePercent);
337 337
                    professor.setShareId(rs.getLong(14));
338
                    String phone = rs.getString(15);
339
                    if(rs.wasNull()){
340
                    	phone = null;
341
                    }
342
                    professor.setPhone(phone);
343
                    String email = rs.getString(16);
344
                    if(rs.wasNull()){
345
                    	email = null;
346
                    }
347
                    professor.setEmail(email);
348
                    
338 349
                    return professor;
339 350
                }else{
340 351
                    return null;

+ 106 - 21
src/main/java/com/ekexiu/push/service/PushService.java

@ -11,6 +11,7 @@ import java.security.cert.CertificateException;
11 11
import java.security.cert.X509Certificate;
12 12
import java.util.HashMap;
13 13
import java.util.Map;
14
import java.util.UUID;
14 15
import java.util.concurrent.ConcurrentHashMap;
15 16

16 17
import javax.net.ssl.HostnameVerifier;
@ -23,6 +24,7 @@ import javax.net.ssl.X509TrustManager;
23 24
import org.jfw.apt.web.annotation.Path;
24 25
import org.jfw.apt.web.annotation.operate.Post;
25 26
import org.jfw.util.ConstData;
27
import org.jfw.util.StringUtil;
26 28
import org.jfw.util.io.IoUtil;
27 29
import org.jfw.util.json.JsonService;
28 30
import org.jfw.util.reflect.TypeReference;
@ -35,14 +37,14 @@ public class PushService {
35 37
	}.getType();
36 38

37 39
	private static Map<String, String> CONTENT_TYPE = new HashMap<String, String>();
38
	private static Map<String,String>  TOKEN_MAP = new ConcurrentHashMap<String,String>();
40
	private static Map<String, String> TOKEN_MAP = new ConcurrentHashMap<String, String>();
39 41
	private static Map<String, String> ERR_MAP = new HashMap<String, String>();
40 42

41 43
	final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
42 44
	// UiIGJTfKNR8oXb8apYdGh3
43
	private String secret = "UiIGJTfKNR8oXb8apYdGh3";
44
	private String appKey = "FnwUFkK2nt9BLgQ0XjtOl4";
45
	private String appId = "G3L3npt6LU92DZHkdNxuL4";
45
	private String secret = "U96j677DHoA1Pv5X0mxMU9";
46
	private String appKey = "48nq7d7yHu8dgbzVHuisp6";
47
	private String appId = "TUdvbnxu1c97r6Fb6cUy57";
46 48
	private int timeout = 0;
47 49

48 50
	private String token = null;
@ -108,16 +110,21 @@ public class PushService {
108 110
		log.error("调用用个推restApi,返回结果错误:" + obj + ":" + errMsg);
109 111
		throw new RuntimeException("调用用个推restApi,返回结果错误:" + obj + ":" + errMsg);
110 112
	}
111
	
112
	private void checkToken()
113
	{
114
		if(this.token==null){
113

114
	private void checkToken() {
115
		if (this.token == null) {
115 116
			log.error("token is null");
116 117
			throw new RuntimeException("token is null");
117 118
		}
118 119
	}
119 120

121
	public String allocateRequesstId() {
122
		UUID uuid = UUID.randomUUID();
123
		return "A" + Long.toString(uuid.getLeastSignificantBits(), 36) + "_" + Long.toString(uuid.getMostSignificantBits(), 36);
124
	}
125

120 126
	private void buildToken() {
127
		if(true) return;
121 128
		long time = System.currentTimeMillis();
122 129
		String sign = this.buildSign(time);
123 130
		StringBuilder sb = new StringBuilder();
@ -138,13 +145,13 @@ public class PushService {
138 145
		}
139 146
	}
140 147

141
	public void refresh(){
142
		if((this.token!=null)  && ((System.currentTimeMillis() -  this.lastBuildTime) < (24*60 *60*1000-5000))){
143
			return ;
148
	public void refresh() {
149
		if ((this.token != null) && ((System.currentTimeMillis() - this.lastBuildTime) < (24 * 60 * 60 * 1000 - 5000))) {
150
			return;
144 151
		}
145 152
		this.buildToken();
146 153
	}
147
	
154

148 155
	public Map<String, Object> post(String url, Map<String, String> header, byte[] data) throws IOException {
149 156
		HttpURLConnection connection = getConnection(url);
150 157
		try {
@ -195,26 +202,93 @@ public class PushService {
195 202
		}
196 203
	}
197 204

198
	
199
	
200 205
	@Path("/bindAlias")
201 206
	@Post
202
	public boolean bindAlias(String alias,String cid){
203
		String url = "https://restapi.getui.com/v1/"+this.appId+"/bind_alias";
207
	public boolean bindAlias(String alias, String cid) {
208
		String url = "https://restapi.getui.com/v1/" + this.appId + "/bind_alias";
204 209
		StringBuilder sb = new StringBuilder();
205 210
		sb.append("{\"alias_list\" : [{\"cid\":\"").append(cid).append("\",\"alias\":\"").append(alias).append("\" }]}");
206 211
		try {
207
			Map<String,Object> ret = post(url, TOKEN_MAP,sb.toString().getBytes(ConstData.UTF8));
212
			Map<String, Object> ret = post(url, TOKEN_MAP, sb.toString().getBytes(ConstData.UTF8));
208 213
			this.checkResult(ret);
209 214
			return true;
210 215
		} catch (IOException e) {
211
			log.error("bind alias error[cid:"+cid+",alias:"+alias+"]",e);
216
			log.error("bind alias error[cid:" + cid + ",alias:" + alias + "]", e);
212 217
			return false;
213 218
		}
214 219
	}
215 220
	
221
	private  Map<String,Object > createIosMessage(String title,String content,String payload){
222
		Map<String,Object> ret = new HashMap<String,Object>();
223
		Map<String,Object> message= new HashMap<String,Object>();
224
		ret.put("message", message);
225
		message.put("appkey",this.appKey);
226
		message.put("is_offline",false);
227
		message.put("msgtype", "transmission");
228
		Map<String,Object> transmission = new HashMap<String,Object>();
229
		ret.put("transmission", transmission);
230
		transmission.put("transmission_type", false);
231
		transmission.put("transmission_content", "I"+payload);
232
		Map<String,Object> pushInfo = new HashMap<String,Object>();
233
		ret.put("push_info", pushInfo);
234
		Map<String,Object> aps = new HashMap<String,Object>();
235
		pushInfo.put("aps",aps);
236
		Map<String,Object> alert = new HashMap<String,Object>();
237
		aps.put("alert", alert);
238
		alert.put("title", title);
239
		alert.put("body",content);
240
		aps.put("autoBadge","+1");
241
		aps.put("content-available",1);
242
		aps.put("sound","default");
243
		pushInfo.put("payload","I"+payload);		
244
		return ret;		
245
	}
246
	private  Map<String,Object > createAndriodMessage(String title,String content,String payload){
247
		Map<String,Object> ret = new HashMap<String,Object>();
248
		Map<String,Object> message= new HashMap<String,Object>();
249
		ret.put("message", message);
250
		message.put("appkey",this.appKey);
251
		message.put("is_offline",false);
252
		message.put("msgtype", "notification");
253
		Map<String,Object> notification = new HashMap<String,Object>();
254
		ret.put("notification", notification);
255
		Map<String,Object> style = new HashMap<String,Object>();
256
		notification.put("style", style);
257
		style.put("type", 0);
258
		style.put("text", content);
259
		style.put("title",title);
260
		
261
		notification.put("transmission_type", false);
262
		notification.put("transmission_content", "A"+payload);
263
		return ret;		
264
	}
216 265
	
217
	
266

267

268
	public void toApp(String title,String content,Object payload){
269
		String url = "https://restapi.getui.com/v1/"+this.appId+"/push_app";
270
		String pl_str = JsonService.toJson(payload);
271
		//Map<String,Object> andriodMessage = this.createAndriodMessage(title, content, pl_str);
272
		Map<String,Object> iosMessage = this.createIosMessage(title, content, pl_str);
273
		//andriodMessage.put("requestid",this.allocateRequesstId());
274
		iosMessage.put("requestid",this.allocateRequesstId());		
275
		
276
//		System.out.println(JsonService.toJson(andriodMessage));
277
		System.out.println(JsonService.toJson(iosMessage));
278
//		try {
279
//			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(andriodMessage).getBytes(ConstData.UTF8));
280
//			this.checkResult(ret);
281
//		} catch (Exception e) {
282
//			log.error("send app message error",e);
283
//		}
284
		try {
285
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(iosMessage).getBytes(ConstData.UTF8));
286
			this.checkResult(ret);
287
		} catch (Exception e) {
288
			log.error("send app message error",e);
289
		}
290
	}
291

218 292
	public static HttpURLConnection getConnection(String urlString) throws IOException {
219 293
		URL url = new URL(urlString);
220 294
		HttpURLConnection conn = null;
@ -297,7 +371,18 @@ public class PushService {
297 371

298 372
	public static void main(String[] args) throws Exception {
299 373
		PushService s = new PushService();
300
		s.buildToken();
301
		s.checkToken();
374
//		 s.buildToken();
375
//		 s.checkToken();
376
		TOKEN_MAP.put("authtoken", "4119677018b45c49d6ff6350e7bea33e81ead059061c50455dda2237938f5bf5");
377
		long l = System.currentTimeMillis();
378
		s.toApp("title_" + l, "content_" + l, "payload_" + l);
379

380
		// for(int i = 0 ;i < 100;++i){
381
		// UUID uuid= UUID.randomUUID();
382
		// System.out.println( "A"+Long.toString(
383
		// uuid.getLeastSignificantBits(),36)+"_"+Long.toString(uuid.getMostSignificantBits(),
384
		// 36));
385
		// }
386

302 387
	}
303 388
}