55
66import cn .jpush .api .JPushClient ;
77import cn .jpush .api .push .PushResult ;
8+ import cn .jpush .api .push .model .Message ;
89import cn .jpush .api .push .model .Platform ;
910import cn .jpush .api .push .model .PushPayload ;
1011import cn .jpush .api .push .model .audience .Audience ;
12+ import cn .jpush .api .push .model .audience .AudienceTarget ;
1113import cn .jpush .api .push .model .notification .AndroidNotification ;
14+ import cn .jpush .api .push .model .notification .IosNotification ;
1215import cn .jpush .api .push .model .notification .Notification ;
13- import cn .jpush .api .report .ReceivedsResult ;
1416
15- public class JPushClientExample {
16- protected static final Logger LOG = LoggerFactory .getLogger (JPushClientExample .class );
17+ public class PushExample {
18+ protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
1719
1820 // demo App defined in resources/jpush-api.conf
1921 private static final String appKey ="dd1066407b044738b6479275" ;
2022 private static final String masterSecret = "2b38ce69b1de2a7fa95706ea" ;
2123
2224 public static final String TITLE = "Test from API example" ;
23- public static final String CONTENT = "Test from Javen" ;
25+ public static final String ALERT = "Test from API Example - alert" ;
26+ public static final String MSG_CONTENT = "Test from API Example - msgContent" ;
2427 public static final String REGISTRATION_ID = "0900e8d85ef" ;
2528 public static final String TAG = "tag_api" ;
2629
2730 public static void main (String [] args ) {
2831 testSendPush ();
29- testGetReport ();
3032 }
3133
32- private static void testSendPush () {
34+
35+ public static void testSendPush () {
3336 JPushClient jpushClient = new JPushClient (masterSecret , appKey );
37+
38+ // For push, all you need do is to build PushPayload object.
3439 PushPayload payload = buildPushObject_all_all_alert ();
3540 LOG .info ("Paylaod JSON - " + payload .toString ());
3641
@@ -47,14 +52,14 @@ private static void testSendPush() {
4752 }
4853
4954 public static PushPayload buildPushObject_all_all_alert () {
50- return PushPayload .alertAll (CONTENT );
55+ return PushPayload .alertAll (ALERT );
5156 }
5257
5358 public static PushPayload buildPushObject_all_alias_alert () {
5459 return PushPayload .newBuilder ()
5560 .setPlatform (Platform .all ())
5661 .setAudience (Audience .alias ("alias1" ))
57- .setNotification (Notification .alert (CONTENT ))
62+ .setNotification (Notification .alert (ALERT ))
5863 .build ();
5964 }
6065
@@ -64,33 +69,40 @@ public static PushPayload buildPushObject_android_tag_alertWithTitle() {
6469 .setAudience (Audience .tag ("tag1" ))
6570 .setNotification (Notification .newBuilder ()
6671 .addPlatformNotification (AndroidNotification .newBuilder ()
67- .setAlert (CONTENT )
72+ .setAlert (ALERT )
6873 .setTitle (TITLE )
6974 .build ())
7075 .build ())
7176 .build ();
7277 }
7378
79+ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtras () {
80+ return PushPayload .newBuilder ()
81+ .setPlatform (Platform .ios ())
82+ .setAudience (Audience .tag_and ("tag1" , "tag_all" ))
83+ .setNotification (Notification .newBuilder ()
84+ .addPlatformNotification (IosNotification .newBuilder ()
85+ .setAlert (ALERT )
86+ .addExtra ("news_id" , 333 )
87+ .build ())
88+ .build ())
89+ .build ();
90+ }
91+
92+ public static PushPayload buildPushObject_ios_audienceMore_message () {
93+ return PushPayload .newBuilder ()
94+ .setPlatform (Platform .ios ())
95+ .setAudience (Audience .newBuilder ()
96+ .addAudienceTarget (AudienceTarget .tag ("tag1" , "tag2" ))
97+ .addAudienceTarget (AudienceTarget .alias ("alias1" , "alias2" ))
98+ .build ())
99+ .setMessage (Message .newBuilder ()
100+ .setMsgContent (MSG_CONTENT )
101+ .addExtra ("news_id" , 333 )
102+ .build ())
103+ .build ();
104+ }
105+
74106
75- public static void testGetReport () {
76- JPushClient jpushClient = new JPushClient (masterSecret , appKey );
77- ReceivedsResult receivedsResult = jpushClient .getReportReceiveds ("1708010723,1774452771" );
78- LOG .debug ("responseContent - " + receivedsResult .getOriginalContent ());
79- if (receivedsResult .isResultOK ()) {
80- LOG .info ("Receiveds - " + receivedsResult );
81- } else {
82- if (receivedsResult .getErrorCode () > 0 ) {
83- // 业务异常
84- LOG .warn ("Service error - ErrorCode: "
85- + receivedsResult .getErrorCode () + ", ErrorMessage: "
86- + receivedsResult .getErrorMessage ());
87- } else {
88- // 未到达 JPush
89- LOG .error ("Other excepitons - "
90- + receivedsResult .getExceptionString ());
91- }
92- }
93- }
94-
95107}
96108
0 commit comments