admin
2021-02-19 58577bae968f2a10232bc8b3c04910b93ea3c69a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package com.tencent.xinge;
 
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
 
import org.json.JSONArray;
import org.json.JSONObject;
 
 
public class XingeApp {
    
    public static final String RESTAPI_PUSHSINGLEDEVICE = "http://openapi.xg.qq.com/v2/push/single_device";
    public static final String RESTAPI_PUSHSINGLEACCOUNT = "http://openapi.xg.qq.com/v2/push/single_account";
    public static final String RESTAPI_PUSHACCOUNTLIST = "http://openapi.xg.qq.com/v2/push/account_list";
    public static final String RESTAPI_PUSHALLDEVICE = "http://openapi.xg.qq.com/v2/push/all_device";
    public static final String RESTAPI_PUSHTAGS = "http://openapi.xg.qq.com/v2/push/tags_device";
    public static final String RESTAPI_QUERYPUSHSTATUS = "http://openapi.xg.qq.com/v2/push/get_msg_status";
    public static final String RESTAPI_QUERYDEVICECOUNT = "http://openapi.xg.qq.com/v2/application/get_app_device_num";
    public static final String RESTAPI_QUERYTAGS = "http://openapi.xg.qq.com/v2/tags/query_app_tags";
    public static final String RESTAPI_CANCELTIMINGPUSH = "http://openapi.xg.qq.com/v2/push/cancel_timing_task";
    public static final String RESTAPI_BATCHSETTAG = "http://openapi.xg.qq.com/v2/tags/batch_set";
    public static final String RESTAPI_BATCHDELTAG = "http://openapi.xg.qq.com/v2/tags/batch_del";
    public static final String RESTAPI_QUERYTOKENTAGS = "http://openapi.xg.qq.com/v2/tags/query_token_tags";
    public static final String RESTAPI_QUERYTAGTOKENNUM = "http://openapi.xg.qq.com/v2/tags/query_tag_token_num";
    public static final String RESTAPI_CREATEMULTIPUSH = "http://openapi.xg.qq.com/v2/push/create_multipush";
    public static final String RESTAPI_PUSHACCOUNTLISTMULTIPLE = "http://openapi.xg.qq.com/v2/push/account_list_multiple";
    public static final String RESTAPI_PUSHDEVICELISTMULTIPLE = "http://openapi.xg.qq.com/v2/push/device_list_multiple";
    public static final String RESTAPI_QUERYINFOOFTOKEN = "http://openapi.xg.qq.com/v2/application/get_app_token_info";
    public static final String RESTAPI_QUERYTOKENSOFACCOUNT = "http://openapi.xg.qq.com/v2/application/get_app_account_tokens";
    public static final String RESTAPI_DELETETOKENOFACCOUNT = "http://openapi.xg.qq.com/v2/application/del_app_account_tokens";
    public static final String RESTAPI_DELETEALLTOKENSOFACCOUNT = "http://openapi.xg.qq.com/v2/application/del_app_account_all_tokens";
 
    public static final String HTTP_POST = "POST";
    public static final String HTTP_GET = "GET";
    
    public static final int DEVICE_ALL = 0;
    public static final int DEVICE_BROWSER = 1;
    public static final int DEVICE_PC = 2;
    public static final int DEVICE_ANDROID = 3;
    public static final int DEVICE_IOS = 4;
    public static final int DEVICE_WINPHONE = 5;
    
    public static final int IOSENV_PROD = 1;
    public static final int IOSENV_DEV = 2;
    
    public static final long IOS_MIN_ID = 2200000000L;
    
    public static void main(String[] args) {
        System.out.println("Hello xg!");
    }
    
    // 易用的api接口v1.1.4引入
    public static JSONObject pushTokenAndroid(long accessId, String secretKey, String title, String content, String token)
    {
        Message message = new Message();
        message.setType(Message.TYPE_NOTIFICATION);
        message.setTitle(title);
        message.setContent(content);
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        JSONObject ret = xinge.pushSingleDevice(token, message);
        return (ret);
    }
    
    public static JSONObject pushAccountAndroid(long accessId, String secretKey, String title, String content, String account)
    {
        Message message = new Message();
        message.setType(Message.TYPE_NOTIFICATION);
        message.setTitle(title);
        message.setContent(content);
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        JSONObject ret = xinge.pushSingleAccount(0, account, message);
        return (ret);
    }
    
    public static JSONObject pushAllAndroid(long accessId, String secretKey, String title, String content)
    {
        Message message = new Message();
        message.setType(Message.TYPE_NOTIFICATION);
        message.setTitle(title);
        message.setContent(content);
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        JSONObject ret = xinge.pushAllDevice(0, message);
        return (ret);
    }
    
    public static JSONObject pushTagAndroid(long accessId, String secretKey, String title, String content, String tag)
    {
        Message message = new Message();
        message.setType(Message.TYPE_NOTIFICATION);
        message.setTitle(title);
        message.setContent(content);
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        List<String> tagList = new ArrayList<String>();
        tagList.add(tag);
        JSONObject ret = xinge.pushTags(0, tagList, "OR", message);
        return (ret);
    }
    
    public static JSONObject pushTokenIos(long accessId, String secretKey, String content, String token, int env)
    {
        MessageIOS message = new MessageIOS();
        message.setAlert(content);
        message.setBadge(1);
        message.setSound("beep.wav");
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        JSONObject ret = xinge.pushSingleDevice(token, message, env);
        return (ret);
    }
    
    public static JSONObject pushAccountIos(long accessId, String secretKey, String content, String account, int env)
    {
        MessageIOS message = new MessageIOS();
        message.setAlert(content);
        message.setBadge(1);
        message.setSound("beep.wav");
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        JSONObject ret = xinge.pushSingleAccount(0, account, message, env);
        return (ret);
    }
    
    public static JSONObject pushAllIos(long accessId, String secretKey, String content, int env)
    {
        MessageIOS message = new MessageIOS();
        message.setAlert(content);
        message.setBadge(1);
        message.setSound("beep.wav");
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        JSONObject ret = xinge.pushAllDevice(0, message, env);
        return (ret);
    }
    
    public static JSONObject pushTagIos(long accessId, String secretKey, String content, String tag, int env)
    {
        MessageIOS message = new MessageIOS();
        message.setAlert(content);
        message.setBadge(1);
        message.setSound("beep.wav");
        
        XingeApp xinge = new XingeApp(accessId, secretKey);    
        List<String> tagList = new ArrayList<String>();
        tagList.add(tag);
        JSONObject ret = xinge.pushTags(0, tagList, "OR", message, env);
        return (ret);
    }
    
    
    
    // 详细的api接口
    public JSONObject pushSingleDevice(String deviceToken, Message message)
    {
        if (!ValidateMessageType(message))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("multi_pkg", message.getMultiPkg());
        params.put("device_token", deviceToken);
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_PUSHSINGLEDEVICE, params);
    }
    
    public JSONObject pushSingleDevice(String deviceToken, MessageIOS message, int environment)
    {
        if (!ValidateMessageType(message, environment))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type or environment error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("device_token", deviceToken);
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        params.put("environment", environment);
        
        if (message.getLoopInterval() > 0 && message.getLoopTimes() > 0) 
        {
            params.put("loop_interval", message.getLoopInterval());
            params.put("loop_times", message.getLoopTimes());
        }
        
        return callRestful(XingeApp.RESTAPI_PUSHSINGLEDEVICE, params);
    }
    
    public JSONObject pushSingleAccount(int deviceType, String account, Message message)
    {
        if (!ValidateMessageType(message))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("multi_pkg", message.getMultiPkg());
        params.put("device_type", deviceType);
        params.put("account", account);
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_PUSHSINGLEACCOUNT, params);
    }
    
    public JSONObject pushAccountList(int deviceType, List<String> accountList, Message message)
    {
        if (!ValidateMessageType(message))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("multi_pkg", message.getMultiPkg());
        params.put("device_type", deviceType);
        params.put("account_list", new JSONArray(accountList).toString());
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_PUSHACCOUNTLIST, params);
    }
    
    public JSONObject pushSingleAccount(int deviceType, String account, MessageIOS message, int environment)
    {
        if (!ValidateMessageType(message, environment))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type or environment error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("device_type", deviceType);
        params.put("account", account);
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        params.put("environment", environment);
        
        return callRestful(XingeApp.RESTAPI_PUSHSINGLEACCOUNT, params);
    }
    
    public JSONObject pushAccountList(int deviceType, List<String> accountList, MessageIOS message, int environment)
    {
        if (!ValidateMessageType(message, environment))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type or environment error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("device_type", deviceType);
        params.put("account_list", new JSONArray(accountList).toString());
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        params.put("environment", environment);
        
        return callRestful(XingeApp.RESTAPI_PUSHACCOUNTLIST, params);
    }
    
    public JSONObject pushAllDevice(int deviceType, Message message)
    {
        if (!ValidateMessageType(message))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("multi_pkg", message.getMultiPkg());
        params.put("device_type", deviceType);
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        if (message.getLoopInterval() > 0 && message.getLoopTimes() > 0) 
        {
            params.put("loop_interval", message.getLoopInterval());
            params.put("loop_times", message.getLoopTimes());
        }
        
        return callRestful(XingeApp.RESTAPI_PUSHALLDEVICE, params);
    }
    
    public JSONObject pushAllDevice(int deviceType, MessageIOS message, int environment)
    {
        if (!ValidateMessageType(message, environment))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type or environment error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("device_type", deviceType);
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        params.put("environment", environment);
        
        if (message.getLoopInterval() > 0 && message.getLoopTimes() > 0) 
        {
            params.put("loop_interval", message.getLoopInterval());
            params.put("loop_times", message.getLoopTimes());
        }
        
        return callRestful(XingeApp.RESTAPI_PUSHALLDEVICE, params);
    }
    
    public JSONObject pushTags(int deviceType, List<String> tagList, String tagOp, Message message)
    {
        if (!ValidateMessageType(message))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type error!'}");
        }
        if (!message.isValid() || tagList.size()==0 || (!tagOp.equals("AND") && !tagOp.equals("OR")))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'param invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("multi_pkg", message.getMultiPkg());
        params.put("device_type", deviceType);
        params.put("message_type", message.getType());
        params.put("tags_list", new JSONArray(tagList).toString());
        params.put("tags_op", tagOp);
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        if (message.getLoopInterval() > 0 && message.getLoopTimes() > 0) 
        {
            params.put("loop_interval", message.getLoopInterval());
            params.put("loop_times", message.getLoopTimes());
        }
        
        return callRestful(XingeApp.RESTAPI_PUSHTAGS, params);
    }
    
    public JSONObject pushTags(int deviceType, List<String> tagList, String tagOp, MessageIOS message, int environment)
    {
        if (!ValidateMessageType(message, environment))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type or environment error!'}");
        }
        if (!message.isValid() || tagList.size()==0 || (!tagOp.equals("AND") && !tagOp.equals("OR")))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'param invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("send_time", message.getSendTime());
        params.put("device_type", deviceType);
        params.put("message_type", message.getType());
        params.put("tags_list", new JSONArray(tagList).toString());
        params.put("tags_op", tagOp);
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        params.put("environment", environment);
        
        if (message.getLoopInterval() > 0 && message.getLoopTimes() > 0) 
        {
            params.put("loop_interval", message.getLoopInterval());
            params.put("loop_times", message.getLoopTimes());
        }
        
        return callRestful(XingeApp.RESTAPI_PUSHTAGS, params);
    }
 
    public JSONObject createMultipush(Message message)
    {
        if (!ValidateMessageType(message))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("multi_pkg", message.getMultiPkg());
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
 
        return callRestful(XingeApp.RESTAPI_CREATEMULTIPUSH, params);
    }
 
    public JSONObject createMultipush(MessageIOS message, int environment)
    {
        if (!ValidateMessageType(message, environment))
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message type or environment error!'}");
        }
        if (!message.isValid())
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'message invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("expire_time", message.getExpireTime());
        params.put("message_type", message.getType());
        params.put("message", message.toJson());
        params.put("timestamp", System.currentTimeMillis()/1000);
        params.put("environment", environment);
 
        return callRestful(XingeApp.RESTAPI_CREATEMULTIPUSH, params);
    }
 
    public JSONObject pushAccountListMultiple(long pushId, List<String> accountList)
    {
        if (pushId <= 0)
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'pushId invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("push_id", pushId);
        params.put("account_list", new JSONArray(accountList).toString());
        params.put("timestamp", System.currentTimeMillis()/1000);
 
        return callRestful(XingeApp.RESTAPI_PUSHACCOUNTLISTMULTIPLE, params);
    }
 
    public JSONObject pushDeviceListMultiple(int pushId, List<String> deviceList)
    {
        if (pushId <= 0)
        {
            return new JSONObject("{'ret_code':-1,'err_msg':'pushId invalid!'}");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("push_id", pushId);
        params.put("device_list", new JSONArray(deviceList).toString());
        params.put("timestamp", System.currentTimeMillis()/1000);
 
        return callRestful(XingeApp.RESTAPI_PUSHDEVICELISTMULTIPLE, params);
    }
 
    public JSONObject queryPushStatus(List<String> pushIdList)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("timestamp", System.currentTimeMillis()/1000);
        JSONArray jArray = new JSONArray();
        for (String pushId : pushIdList) {
            JSONObject js = new JSONObject();
            js.put("push_id", pushId);
            jArray.put(js);
        }
        params.put("push_ids", jArray.toString());
        
        return callRestful(XingeApp.RESTAPI_QUERYPUSHSTATUS, params);
    }
    
    public JSONObject queryDeviceCount()
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_QUERYDEVICECOUNT, params);
    }
    
    public JSONObject queryTags(int start, int limit)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("start", start);
        params.put("limit", limit);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_QUERYTAGS, params);
    }
    
    public JSONObject queryTags()
    {        
        return queryTags(0,100);
    }
    
    public JSONObject queryTagTokenNum(String tag)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("tag", tag);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_QUERYTAGTOKENNUM, params);
    }
    
    public JSONObject queryTokenTags(String deviceToken)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("device_token", deviceToken);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_QUERYTOKENTAGS, params);
    }
    
    public JSONObject cancelTimingPush(String pushId)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("push_id", pushId);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_CANCELTIMINGPUSH, params);
    }
    
    public XingeApp(long accessId, String secretKey)
    {
        this.m_accessId = accessId;
        this.m_secretKey = secretKey;
    }
    
    protected String generateSign(String method, String url, Map<String, Object> params)
    {
        List<Map.Entry<String, Object>> paramList = new ArrayList<Map.Entry<String, Object>>(params.entrySet());
        Collections.sort(paramList, new Comparator<Map.Entry<String, Object>>() {   
            public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                return (o1.getKey()).toString().compareTo(o2.getKey());
            }
        });
        String paramStr = ""; String md5Str = "";
        for (Map.Entry<String, Object> entry : paramList) {
            paramStr += entry.getKey() + "=" + entry.getValue().toString();
        }
        try
        {
            URL u = new URL(url);
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            String s = method+u.getHost()+u.getPath()+paramStr+this.m_secretKey;
            byte[] bArr = s.getBytes("UTF-8");
            byte[] md5Value = md5.digest(bArr);
            BigInteger bigInt = new BigInteger(1,md5Value);
            md5Str = bigInt.toString(16);
            while(md5Str.length() < 32 ){
                md5Str = "0"+md5Str;
            }
            //System.out.println(s);
        }
        catch(Exception e)
        {
            //e.printStackTrace();
            return "";
        }
        return md5Str;
    }
    
    protected JSONObject callRestful(String url, Map<String, Object> params) 
    {
        String temp;
        String ret="";
        JSONObject jsonRet=null;
        String sign = generateSign("POST", url, params);
        if(sign.isEmpty()) return new JSONObject("{\"ret_code\":-1,\"err_msg\":\"generateSign error\"}");
        params.put("sign", sign);
        try{
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)u.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(10000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            StringBuffer param = new StringBuffer();
            for (String key: params.keySet())
            {
                param.append(key).append("=").append(URLEncoder.encode(params.get(key).toString(), "UTF-8")).append("&");
            }
            conn.getOutputStream().write(param.toString().getBytes("UTF-8"));
            //System.out.println(param);
            conn.getOutputStream().flush();
            conn.getOutputStream().close();
            InputStreamReader isr = new InputStreamReader(conn.getInputStream());  
            BufferedReader br = new BufferedReader(isr);  
            while((temp = br.readLine()) != null){  
                ret += temp;  
            }     
            br.close();  
            isr.close();
            conn.disconnect();
            //System.out.println(ret);
            jsonRet = new JSONObject(ret);
            
        } catch(java.net.SocketTimeoutException e) {
            //e.printStackTrace();
            jsonRet = new JSONObject("{\"ret_code\":-1,\"err_msg\":\"call restful timeout\"}");
        } catch(Exception e) {
            //e.printStackTrace();
            jsonRet = new JSONObject("{\"ret_code\":-1,\"err_msg\":\"call restful error\"}");
        }
        return jsonRet;
    }
 
    protected boolean ValidateToken(String token) {
        if (this.m_accessId >= 2200000000L) {
            return token.length() == 64;
        } else {
            return (token.length() == 40 || token.length() == 64);
        }
    }
 
    protected Map<String, Object> InitParams() {
        Map<String, Object> params = new HashMap<String, Object>();
 
        params.put("access_id", this.m_accessId);
        params.put("timestamp", System.currentTimeMillis()/1000);
 
        return params;
    }
 
    public JSONObject BatchSetTag(List<TagTokenPair> tagTokenPairs) 
    {
 
        for (TagTokenPair pair : tagTokenPairs) {     
            if (!this.ValidateToken(pair.token)) {
                String returnMsgJsonStr = String.format("{\"ret_code\":-1,\"err_msg\":\"invalid token %s\"}", pair.token);
                return new JSONObject(returnMsgJsonStr);
            }
        }
 
        Map<String, Object> params = this.InitParams();
 
        List<List> tag_token_list = new ArrayList<List>();
 
        for (TagTokenPair pair : tagTokenPairs) {
            List<String> singleTagToken = new ArrayList<String>();
            singleTagToken.add(pair.tag);
            singleTagToken.add(pair.token);
 
            tag_token_list.add(singleTagToken);
        }
 
        params.put("tag_token_list", new JSONArray(tag_token_list).toString());
          
          return callRestful(XingeApp.RESTAPI_BATCHSETTAG, params); 
    }
    
    public JSONObject BatchDelTag(List<TagTokenPair> tagTokenPairs) 
    {
 
        for (TagTokenPair pair : tagTokenPairs) {     
            if (!this.ValidateToken(pair.token)) {
                String returnMsgJsonStr = String.format("{\"ret_code\":-1,\"err_msg\":\"invalid token %s\"}", pair.token);
                return new JSONObject(returnMsgJsonStr);
            }
        }
 
        Map<String, Object> params = this.InitParams();
 
        List<List> tag_token_list = new ArrayList<List>();
 
        for (TagTokenPair pair : tagTokenPairs) {
            List<String> singleTagToken = new ArrayList<String>();
            singleTagToken.add(pair.tag);
            singleTagToken.add(pair.token);
 
            tag_token_list.add(singleTagToken);
        }
 
        params.put("tag_token_list", new JSONArray(tag_token_list).toString());
          
          return callRestful(XingeApp.RESTAPI_BATCHDELTAG, params); 
    }
 
    public JSONObject queryInfoOfToken(String deviceToken)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("device_token", deviceToken);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_QUERYINFOOFTOKEN, params);
    }
 
    public JSONObject queryTokensOfAccount(String account)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("account", account);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_QUERYTOKENSOFACCOUNT, params);
    }
    
    public JSONObject deleteTokenOfAccount(String account, String deviceToken)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("account", account);
        params.put("device_token", deviceToken);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_DELETETOKENOFACCOUNT, params);
    }
    
    public JSONObject deleteAllTokensOfAccount(String account)
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("access_id", this.m_accessId);
        params.put("account", account);
        params.put("timestamp", System.currentTimeMillis()/1000);
        
        return callRestful(XingeApp.RESTAPI_DELETEALLTOKENSOFACCOUNT, params);
    }
    
    protected boolean ValidateMessageType(Message message)
    {
        if (this.m_accessId < IOS_MIN_ID)
            return true;
        else 
            return false;
    }
    
    protected boolean ValidateMessageType(MessageIOS message, int environment)
    {
        if (this.m_accessId >= IOS_MIN_ID && (environment == IOSENV_PROD || environment == IOSENV_DEV))
            return true;
        else 
            return false;
    }
    
    private long m_accessId;
    private String m_secretKey;
}