1616import cn .jpush .api .push .model .audience .Audience ;
1717import cn .jpush .api .push .model .audience .AudienceTarget ;
1818import cn .jpush .api .push .model .notification .*;
19+ import cn .jpush .api .report .GroupMessageDetailResult ;
1920import com .google .gson .Gson ;
2021import com .google .gson .GsonBuilder ;
2122import com .google .gson .JsonObject ;
3132public class PushExample {
3233 protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
3334
34- // demo App defined in resources/jpush-api.conf
35+ /**
36+ * Change the app key and master secret to your own account
37+ * If you want to use push by group, please enter your own group push key and group master secret.
38+ */
3539 protected static final String APP_KEY = "8f02a4fa717a6235734d92de" ;
3640 protected static final String MASTER_SECRET = "cf6de29f9e66432ba4ac1c32" ;
3741 protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c" ;
@@ -56,6 +60,9 @@ public static void main(String[] args) {
5660
5761 // 目前推荐这个方法进行测试
5862 testSendPush ();
63+ // testSendGroupPush();
64+
65+
5966// testGetCidList();
6067// testSendPushes();
6168// testSendPush_fromJSON();
@@ -229,7 +236,7 @@ public void run() {
229236 }
230237 }
231238
232- public void testSendGroupPush () {
239+ public static void testSendGroupPush () {
233240 GroupPushClient groupPushClient = new GroupPushClient (GROUP_MASTER_SECRET , GROUP_PUSH_KEY );
234241 final PushPayload payload = buildPushObject_android_and_ios ();
235242 try {
@@ -239,9 +246,9 @@ public void testSendGroupPush() {
239246 PushResult pushResult = entry .getValue ();
240247 PushResult .Error error = pushResult .error ;
241248 if (error != null ) {
242- LOG .info ("AppKey: " + entry .getKey () + " error code : " + error .getCode () + " error message: " + error .getMessage ());
249+ LOG .info ("Group_msgid: " + groupPushResult . getGroupMsgId () + " AppKey: " + entry .getKey () + " error code : " + error .getCode () + " error message: " + error .getMessage ());
243250 } else {
244- LOG .info ("AppKey: " + entry .getKey () + " sendno: " + pushResult .sendno + " msg_id:" + pushResult .msg_id );
251+ LOG .info ("Group_msgid: " + groupPushResult . getGroupMsgId () + " AppKey: " + entry .getKey () + " sendno: " + pushResult .sendno + " msg_id:" + pushResult .msg_id );
245252 }
246253
247254 }
@@ -279,6 +286,11 @@ public static PushPayload buildPushObject_android_tag_alertWithTitle() {
279286 .build ();
280287 }
281288
289+ /**
290+ * Could modify the contents for pushing
291+ * The comments are showing how to use it
292+ * @return
293+ */
282294 public static PushPayload buildPushObject_android_and_ios () {
283295 Map <String , String > extras = new HashMap <String , String >();
284296 extras .put ("test" , "https://community.jiguang.cn/push" );
@@ -297,9 +309,6 @@ public static PushPayload buildPushObject_android_and_ios() {
297309 .addPlatformNotification (IosNotification .newBuilder ()
298310 .incrBadge (1 )
299311 .addExtra ("extra_key" , "extra_value" ).build ())
300- .addVoip ("key" , "value" )
301- .addVoip ("key2" , 2 )
302- .addVoip ("key3" , true )
303312 .build ())
304313// .setSMS(SMS.newBuilder()
305314// .setDelayTime(1000)
@@ -450,6 +459,7 @@ public static PushPayload buildPushObject_android_cid() {
450459 list .add ("1507ffd3f79545957de" );
451460 list .add ("1507ffd3f79457957de" );
452461 list .add ("1507ffd3f79456757de" );
462+ list .add ("zzzzzzzz" );
453463
454464
455465 return PushPayload .newBuilder ()
@@ -592,29 +602,29 @@ public static void testBatchSend() {
592602 LOG .info ("batchSendPushByAlias param: {}, result: {}" , pushPayloadList , new Gson ().toJson (result .getBatchPushResult ()));
593603 }
594604
595- {
596- List <PushPayload > pushPayloadList = new ArrayList <>();
597- PushPayload .Builder builder1 = PushPayload .newBuilder ();
598- builder1 .setMessage (Message .content ("content1 by regId" ))
599- .setNotification (Notification .alert (ALERT ))
600- .setPlatform (Platform .android ())
601- .setAudience (Audience .all ())
602- .setOptions (Options .sendno ())
603- .setTarget ("1507ffd3f79456757de" );
604- pushPayloadList .add (builder1 .build ());
605-
606- PushPayload .Builder builder2 = PushPayload .newBuilder ();
607- builder2 .setMessage (Message .content ("content2 by regId" ))
608- .setNotification (Notification .alert (ALERT ))
609- .setAudience (Audience .all ())
610- .setPlatform (Platform .ios ())
611- .setOptions (Options .sendno ())
612- .setTarget ("1507ffd3f79456757de" );
613- pushPayloadList .add (builder2 .build ());
614-
615- BatchPushResult result = jPushClient .batchSendPushByRegId (pushPayloadList );
616- LOG .info ("batchSendPushByRegId param: {}, result: {}" , pushPayloadList , new Gson ().toJson (result .getBatchPushResult ()));
617- }
605+ // {
606+ // List<PushPayload> pushPayloadList = new ArrayList<>();
607+ // PushPayload.Builder builder1 = PushPayload.newBuilder();
608+ // builder1.setMessage(Message.content("content1 by regId"))
609+ // .setNotification(Notification.alert(ALERT))
610+ // .setPlatform(Platform.android())
611+ // .setAudience(Audience.all())
612+ // .setOptions(Options.sendno())
613+ // .setTarget("1507ffd3f79456757de");
614+ // pushPayloadList.add(builder1.build());
615+ //
616+ // PushPayload.Builder builder2 = PushPayload.newBuilder();
617+ // builder2.setMessage(Message.content("content2 by regId"))
618+ // .setNotification(Notification.alert(ALERT))
619+ // .setAudience(Audience.all())
620+ // .setPlatform(Platform.ios())
621+ // .setOptions(Options.sendno())
622+ // .setTarget("1507ffd3f79456757de");
623+ // pushPayloadList.add(builder2.build());
624+ //
625+ // BatchPushResult result = jPushClient.batchSendPushByRegId(pushPayloadList);
626+ // LOG.info("batchSendPushByRegId param: {}, result: {}", pushPayloadList, new Gson().toJson(result.getBatchPushResult()));
627+ // }
618628
619629 } catch (APIConnectionException e ) {
620630 LOG .error ("Connection error. Should retry later. " , e );
0 commit comments