jiapeng 7 lat temu
rodzic
commit
516bbedb5a

+ 3 - 1
src/main/java/com/ekexiu/operation/push/getui/Message.java

@ -3,10 +3,12 @@ package com.ekexiu.operation.push.getui;
3 3
import com.google.gson.annotations.SerializedName;
4 4

5 5
public class Message {
6
	
6 7
	//注册应用时生成的appkey 
8
	@SerializedName("appkey")
7 9
	private String appKey;
8 10
	//是否离线推送 
9
	@SerializedName("is_offine")
11
	@SerializedName("is_offline")
10 12
	private boolean isOffline = true;
11 13
	//消息应用类型,可选项:notification、link、notypopload、transmission
12 14
	private String msgtype;

+ 34 - 0
src/main/java/com/ekexiu/operation/push/getui/MsgCondition.java

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

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

5
public class MsgCondition {
6
	private String key;
7
	private String[] values;
8
	@SerializedName("opt_type")
9
	private int optType;
10
	public String getKey() {
11
		return key;
12
	}
13
	public void setKey(String key) {
14
		this.key = key;
15
	}
16
	public String[] getValues() {
17
		return values;
18
	}
19
	public void setValues(String[] values) {
20
		this.values = values;
21
	}
22
	public int getOptType() {
23
		return optType;
24
	}
25
	public void setOptType(int optType) {
26
		this.optType = optType;
27
	}
28
	public MsgCondition(String key,String[] values,int optType){
29
		this.key = key;
30
		this.values = values;
31
		this.optType = optType;
32
	}
33
	
34
}

+ 2 - 0
src/main/java/com/ekexiu/portal/dao/PpatentDao.java

@ -7,6 +7,7 @@ import java.util.List;
7 7
import org.jfw.apt.annotation.Nullable;
8 8
import org.jfw.apt.orm.annotation.dao.DAO;
9 9
import org.jfw.apt.orm.annotation.dao.method.From;
10
import org.jfw.apt.orm.annotation.dao.method.Or;
10 11
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
11 12
import org.jfw.apt.orm.annotation.dao.method.SetSentence;
12 13
import org.jfw.apt.orm.annotation.dao.method.operator.PageQuery;
@ -63,6 +64,7 @@ public interface PpatentDao {
63 64
	
64 65
	@PageSelect
65 66
	@OrderBy(" ORDER BY SORT_NUM DESC,CREATE_TIME DESC,ID DESC")
67
	@Or
66 68
	PageQueryResult<Ppatent> query(Connection con,@Alias({"code","authors","name","keywords"}) @Like String cnt,int pageSize,int pageNo) throws SQLException;
67 69

68 70
	@SelectOne

+ 173 - 28
src/main/java/com/ekexiu/push/service/PushService.java

@ -22,19 +22,32 @@ import javax.net.ssl.TrustManager;
22 22
import javax.net.ssl.X509TrustManager;
23 23

24 24
import org.jfw.apt.web.annotation.Path;
25
import org.jfw.apt.web.annotation.operate.Get;
25 26
import org.jfw.apt.web.annotation.operate.Post;
26 27
import org.jfw.util.ConstData;
27
import org.jfw.util.StringUtil;
28 28
import org.jfw.util.io.IoUtil;
29 29
import org.jfw.util.json.JsonService;
30 30
import org.jfw.util.reflect.TypeReference;
31 31

32
import com.ekexiu.operation.push.getui.Message;
33
import com.ekexiu.operation.push.getui.MsgCondition;
34
import com.ekexiu.operation.push.getui.MsgStyle;
35
import com.ekexiu.operation.push.getui.NotificationMessage;
36

32 37
@Path("/push")
33 38
public class PushService {
34 39
	private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(PushService.class);
35 40

36 41
	private static Type RES_TYPE = new TypeReference<Map<String, Object>>() {
37 42
	}.getType();
43
	
44
	
45
	private static final MsgCondition  android=new MsgCondition("phonetype",new String[]{"ANDROID"},1);
46
	private static final MsgCondition  ios=new MsgCondition("phonetype",new String[]{"IOS"},1);
47
	
48
	private static final MsgCondition[]  onlyAndroid = new MsgCondition[]{android};
49
	
50
	private static final MsgCondition[]  onlyIos = new MsgCondition[]{ios};
38 51

39 52
	private static Map<String, String> CONTENT_TYPE = new HashMap<String, String>();
40 53
	private static Map<String, String> TOKEN_MAP = new ConcurrentHashMap<String, String>();
@ -218,17 +231,49 @@ public class PushService {
218 231
		}
219 232
	}
220 233
	
234
	@Path("/bindTags")
235
	@Post
236
	public boolean bindTags(String[] tags, String cid) {
237
		String url = "https://restapi.getui.com/v1/" + this.appId + "/set_tags";
238
		Map<String,Object> msg = new HashMap<String,Object>();
239
		msg.put("cid",cid);
240
		msg.put("tag_list",tags);
241
		try {
242
			Map<String, Object> ret = post(url, TOKEN_MAP, JsonService.toJson(msg).getBytes(ConstData.UTF8));
243
			this.checkResult(ret);
244
			return true;
245
		} catch (IOException e) {
246
			log.error("bind tags error:"+JsonService.toJson(msg), e);
247
			return false;
248
		}
249
	}
250
	@Path("/applyBadge")
251
	@Get
252
	public void applyBadge(int badge,String cid,String devtoken){
253
		String url = "https://restapi.getui.com/v1/" + this.appId + "/set_badge";
254
		StringBuilder sb = new StringBuilder();
255
		sb.append("{\"badge\":").append(badge).append(",\"cid_list\":[\"").append(cid).append("\"],\"devicetoken_list\":[\"").append(devtoken).append("\"]}");
256
		System.out.println(sb.toString());
257
		try {
258
			Map<String, Object> ret = post(url, TOKEN_MAP, sb.toString().getBytes(ConstData.UTF8));
259
			this.checkResult(ret);
260
		} catch (IOException e) {
261
			log.error("apply badge error[cid:" + cid + ",devtoken:" + devtoken + "]", e);
262
		}
263
		
264
	}
265
	
221 266
	private  Map<String,Object > createIosMessage(String title,String content,String payload){
222 267
		Map<String,Object> ret = new HashMap<String,Object>();
223 268
		Map<String,Object> message= new HashMap<String,Object>();
224 269
		ret.put("message", message);
225 270
		message.put("appkey",this.appKey);
226
		message.put("is_offline",false);
271
		message.put("is_offline",true);
227 272
		message.put("msgtype", "transmission");
228 273
		Map<String,Object> transmission = new HashMap<String,Object>();
229 274
		ret.put("transmission", transmission);
230 275
		transmission.put("transmission_type", false);
231
		transmission.put("transmission_content", "I"+payload);
276
		transmission.put("transmission_content", payload);
232 277
		Map<String,Object> pushInfo = new HashMap<String,Object>();
233 278
		ret.put("push_info", pushInfo);
234 279
		Map<String,Object> aps = new HashMap<String,Object>();
@ -240,26 +285,34 @@ public class PushService {
240 285
		aps.put("autoBadge","+1");
241 286
		aps.put("content-available",1);
242 287
		aps.put("sound","default");
243
		pushInfo.put("payload","I"+payload);		
288
		pushInfo.put("payload",payload);		
289
		ret.put("condition", onlyIos);
244 290
		return ret;		
245 291
	}
292

293
	
246 294
	private  Map<String,Object > createAndriodMessage(String title,String content,String payload){
247 295
		Map<String,Object> ret = new HashMap<String,Object>();
248
		Map<String,Object> message= new HashMap<String,Object>();
296
		Message message = new Message();
249 297
		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>();
298
		message.setAppKey(this.appKey);
299
		message.setMsgtype("notification");
300
		message.setOffline(true);
301
		message.setOfflineExpireTime(2*60*1000);
302
		message.setPushNetworkType(0);
303
		NotificationMessage notification = new NotificationMessage();
254 304
		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);
305
	    notification.setTransmissionType(true);
306
	    notification.setTransmissionContent(payload);
307
	    MsgStyle style = new MsgStyle();
308
	    notification.setStyle(style);		
309
		style.setType(0);
310
		style.setClearable(true);
311
		style.setRing(true);
312
		style.setText(content);
313
		style.setTitle(title);
314
		style.setVibrate(true);
315
		ret.put("condition", onlyAndroid);
263 316
		return ret;		
264 317
	}
265 318
	
@ -268,19 +321,56 @@ public class PushService {
268 321
	public void toApp(String title,String content,Object payload){
269 322
		String url = "https://restapi.getui.com/v1/"+this.appId+"/push_app";
270 323
		String pl_str = JsonService.toJson(payload);
271
		//Map<String,Object> andriodMessage = this.createAndriodMessage(title, content, pl_str);
324
		Map<String,Object> andriodMessage = this.createAndriodMessage(title, content, pl_str);
272 325
		Map<String,Object> iosMessage = this.createIosMessage(title, content, pl_str);
273
		//andriodMessage.put("requestid",this.allocateRequesstId());
326
		andriodMessage.put("requestid",this.allocateRequesstId());
274 327
		iosMessage.put("requestid",this.allocateRequesstId());		
275 328
		
276 329
//		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
//		}
330
//		System.out.println(JsonService.toJson(iosMessage));
331
		try {
332
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(andriodMessage).getBytes(ConstData.UTF8));
333
			this.checkResult(ret);
334
		} catch (Exception e) {
335
			log.error("send app message error",e);
336
		}
337
		try {
338
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(iosMessage).getBytes(ConstData.UTF8));
339
			this.checkResult(ret);
340
		} catch (Exception e) {
341
			log.error("send app message error",e);
342
		}
343
	}
344
	private MsgCondition createTagCondition(String[] tags){
345
		return new MsgCondition("tag", tags, 0);
346
	}
347
	private void addTag(Map<String,Object> msg,MsgCondition  tagCondition){
348
		MsgCondition[] mcs = (MsgCondition[]) msg.get("condition");
349
		MsgCondition[] mcsn = new MsgCondition[mcs.length+1];
350
		System.arraycopy(mcs,0,mcsn, 0,mcs.length);
351
		mcsn[mcs.length] = tagCondition;
352
		msg.put("condition", mcsn);
353
		
354
	}
355
	
356
	public void pushWithTag(String title,String content,Object payload,String[] tags){
357
		String url = "https://restapi.getui.com/v1/"+this.appId+"/push_app";
358
		String pl_str = JsonService.toJson(payload);
359
		Map<String,Object> andriodMessage = this.createAndriodMessage(title, content, pl_str);
360
		Map<String,Object> iosMessage = this.createIosMessage(title, content, pl_str);
361
		andriodMessage.put("requestid",this.allocateRequesstId());
362
		iosMessage.put("requestid",this.allocateRequesstId());	
363
		
364
		MsgCondition tagC = this.createTagCondition(tags);
365
		addTag(andriodMessage, tagC);
366
		addTag(iosMessage, tagC);
367

368
		try {
369
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(andriodMessage).getBytes(ConstData.UTF8));
370
			this.checkResult(ret);
371
		} catch (Exception e) {
372
			log.error("send app message error",e);
373
		}
284 374
		try {
285 375
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(iosMessage).getBytes(ConstData.UTF8));
286 376
			this.checkResult(ret);
@ -289,6 +379,59 @@ public class PushService {
289 379
		}
290 380
	}
291 381

382
	public void pushWithCid(String title,String content,Object payload,String cid){
383
		String url = "https://restapi.getui.com/v1/"+this.appId+"/push_single";
384
		String pl_str = JsonService.toJson(payload);
385
		Map<String,Object> androidMessage = this.createAndriodMessage(title, content, pl_str);
386
		Map<String,Object> iosMessage = this.createIosMessage(title, content, pl_str);
387
		androidMessage.put("requestid",this.allocateRequesstId());
388
		androidMessage.put("cid",cid);
389
		iosMessage.put("requestid",this.allocateRequesstId());		
390
		iosMessage.put("cid",cid);
391
		
392
//		System.out.println(JsonService.toJson(androidMessage));
393
//		System.out.println(JsonService.toJson(iosMessage));
394
		try {
395
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(androidMessage).getBytes(ConstData.UTF8));
396
			this.checkResult(ret);
397
		} catch (Exception e) {
398
			log.error("send app message error",e);
399
		}
400
		try {
401
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(iosMessage).getBytes(ConstData.UTF8));
402
			this.checkResult(ret);
403
		} catch (Exception e) {
404
			log.error("send app message error",e);
405
		}	
406
	}
407
	public void pushWithAlias(String title,String content,Object payload,String alias){
408
		String url = "https://restapi.getui.com/v1/"+this.appId+"/push_single";
409
		String pl_str = JsonService.toJson(payload);
410
		Map<String,Object> androidMessage = this.createAndriodMessage(title, content, pl_str);
411
		Map<String,Object> iosMessage = this.createIosMessage(title, content, pl_str);
412
		androidMessage.put("requestid",this.allocateRequesstId());
413
		androidMessage.put("alias",alias);
414
		iosMessage.put("requestid",this.allocateRequesstId());		
415
		iosMessage.put("alias",alias);
416
		
417
//		System.out.println(JsonService.toJson(androidMessage));
418
//		System.out.println(JsonService.toJson(iosMessage));
419
		try {
420
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(androidMessage).getBytes(ConstData.UTF8));
421
			this.checkResult(ret);
422
		} catch (Exception e) {
423
			log.error("send app message error",e);
424
		}
425
		try {
426
			Map<String,Object> ret = post(url, TOKEN_MAP,JsonService.toJson(iosMessage).getBytes(ConstData.UTF8));
427
			this.checkResult(ret);
428
		} catch (Exception e) {
429
			log.error("send app message error",e);
430
		}	
431
	}
432
	
433
	
434
	
292 435
	public static HttpURLConnection getConnection(String urlString) throws IOException {
293 436
		URL url = new URL(urlString);
294 437
		HttpURLConnection conn = null;
@ -345,6 +488,7 @@ public class PushService {
345 488
	static {
346 489
		CONTENT_TYPE.put("Content-Type", "application/json");
347 490
		TOKEN_MAP.putAll(CONTENT_TYPE);
491
		TOKEN_MAP.put("authtoken", "ef60d2aa2bc3193fc99a99ef43906cee70658156dfb41156acc35f7e7b7fb3dd");
348 492

349 493
		ERR_MAP.put("no_msg", "没有消息体");
350 494
		ERR_MAP.put("alias_error", "找不到别名");
@ -373,10 +517,11 @@ public class PushService {
373 517
		PushService s = new PushService();
374 518
//		 s.buildToken();
375 519
//		 s.checkToken();
376
		TOKEN_MAP.put("authtoken", "4119677018b45c49d6ff6350e7bea33e81ead059061c50455dda2237938f5bf5");
377
		long l = System.currentTimeMillis();
520
//		TOKEN_MAP.put("authtoken", "ef60d2aa2bc3193fc99a99ef43906cee70658156dfb41156acc35f7e7b7fb3dd");
521
	long l = System.currentTimeMillis();
378 522
		s.toApp("title_" + l, "content_" + l, "payload_" + l);
379 523

524
//	s.applyBadge(1, "f293af1f1cc043185ee9d921b63c4807", "D380AD6865B61F4D179DBB208EE3C7268A672AB9821385050596608A186F6023");
380 525
		// for(int i = 0 ;i < 100;++i){
381 526
		// UUID uuid= UUID.randomUUID();
382 527
		// System.out.println( "A"+Long.toString(