11package cn .jpush .api .examples ;
22
3- import java .util .HashMap ;
4- import java .util .Map ;
5-
63import org .slf4j .Logger ;
74import org .slf4j .LoggerFactory ;
85
96import cn .jpush .api .JPushClient ;
7+ import cn .jpush .api .common .DeviceEnum ;
108import cn .jpush .api .push .CustomMessageParams ;
11- import cn .jpush .api .push .IOSExtra ;
129import cn .jpush .api .push .MessageResult ;
1310import cn .jpush .api .push .ReceiverTypeEnum ;
1411import cn .jpush .api .report .ReceivedsResult ;
@@ -20,105 +17,33 @@ public class JPushClientExample {
2017 private static final String appKey ="dd1066407b044738b6479275" ;
2118 private static final String masterSecret = "2b38ce69b1de2a7fa95706ea" ;
2219
23- private static JPushClient _client = null ;
20+ private static final String msgTitle = "Test from API example" ;
21+ private static final String msgContent = "Test Test" ;
22+ private static final String registrationID = "0900e8d85ef" ;
2423
25- /**
26- * 保存离线的时长。秒为单位。最多支持10天(864000秒)。
27- * 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。
28- * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。
29- */
30- private static long timeToLive = 60 * 60 * 24 ;
24+ private static JPushClient jpushClient = null ;
3125
3226 public static void main (String [] args ) {
33- /*
34- * Example1: 初始化,默认发送给android和ios,同时设置离线消息存活时间
35- * jpush = new JPushClient(masterSecret, appKey, timeToLive);
36- */
37-
38- /*
39- * Example2: 只发送给android
40- * jpush = new JPushClient(masterSecret, appKey, DeviceEnum.Android);
41- */
42-
43- /*
44- * Example3: 只发送给IOS
45- * jpush = new JPushClient(masterSecret, appKey, DeviceEnum.IOS);
46- */
47-
48- /*
49- * Example4: 只发送给android,同时设置离线消息存活时间
50- * jpush = new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.Android);
51- */
52-
53-
54- _client = new JPushClient (masterSecret , appKey );
27+ jpushClient = new JPushClient (masterSecret , appKey , 0 , DeviceEnum .Android , false );
5528
56- /*
57- * 是否启用ssl安全连接, 可选
58- * 参数:启用true, 禁用false,默认为非ssl连接
59- */
60- //jpush.setEnableSSL(true);
61-
62-
63- //测试发送消息或者通知
6429 testSend ();
6530 }
6631
6732 private static void testSend () {
68- // 在实际业务中,建议 sendNo 是一个你自己的业务可以处理的一个自增数字。
69- int sendNo = getRandomSendNo ();
70- String msgTitle = "Test from javen" ;
71- String msgContent = "Test Test" ;
72-
73- /*
74- * IOS设备扩展参数,
75- * 设置badge,设置声音
76- */
77- Map <String , Object > extra = new HashMap <String , Object >();
78- IOSExtra iosExtra = new IOSExtra (10 , "WindowsLogonSound.wav" );
79- extra .put ("ios" , iosExtra );
80-
81- /*
82- * 通知、消息 两者区别。请参考:http://docs.jpush.cn/pages/viewpage.action?pageId=3309701
83- */
84-
85- String regID = "050ffb64f67" ;
86-
8733 CustomMessageParams params = new CustomMessageParams ();
88- params .setReceiverType (ReceiverTypeEnum .TAG );
89- params .setReceiverValue ("nexus7_0986b893" );
90- MessageResult msgResult = _client .sendCustomMessage (msgTitle , msgContent , params , null );
91-
92- //对所有用户发送消息。
93- //MessageResult msgResult = jpush.sendCustomMessageWithAppKey(sendNo,msgTitle, msgContent);
94-
34+ params .setReceiverType (ReceiverTypeEnum .REGISTRATION_ID );
35+ params .setReceiverValue (registrationID );
36+ MessageResult msgResult = jpushClient .sendCustomMessage (msgTitle , msgContent , params , null );
9537
96- //覆盖指定msgId的消息,msgId可以从msgResult.getMsgid()获取。
97- //MessageResult msgResult = jpush.sendNotificationWithAppKey(sendNo, msgTitle, msgContent, 0, extra,msgResult.getMsgid());
98-
99-
10038 if (null != msgResult ) {
101- LOG .info ("content - " + msgResult .responseResult .responseContent );
102- System .out .println ("服务器返回数据: " + msgResult .toString ());
103- } else {
104- System .out .println ("无法获取数据" );
39+ LOG .info ("responseContent - " + msgResult .responseResult .responseContent );
40+ LOG .info ("msgResult - " + msgResult );
10541 }
106-
107- ReceivedsResult rrr = _client .getReportReceiveds ("1708010723,1774452771" );
42+
43+ ReceivedsResult rrr = jpushClient .getReportReceiveds ("1708010723,1774452771" );
10844 LOG .info ("content - " + rrr .responseResult .responseContent );
10945 LOG .debug ("Received - " + rrr );
11046 }
11147
112- public static final int MAX = Integer .MAX_VALUE ;
113- public static final int MIN = (int ) MAX /2 ;
114-
115- /**
116- * 保持 sendNo 的唯一性是有必要的
117- * It is very important to keep sendNo unique.
118- * @return sendNo
119- */
120- public static int getRandomSendNo () {
121- return (int ) (MIN + Math .random () * (MAX - MIN ));
122- }
123-
12448}
49+
0 commit comments