Skip to content

Commit 929e7b5

Browse files
committed
rewrite tesetSendPush_fromJSON example
1 parent fadeb35 commit 929e7b5

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public class PushExample {
3939
public static void main(String[] args) {
4040
// testSendPushWithCustomConfig();
4141
// testSendIosAlert();
42-
testSendPush();
42+
// testSendPush();
43+
testSendPush_fromJSON();
4344
}
4445

4546

@@ -51,16 +52,8 @@ public static void testSendPush() {
5152

5253
// For push, all you need do is to build PushPayload object.
5354
PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
54-
System.out.println("原始String类型payload: " + payload.toString());
55-
Gson gson = new GsonBuilder()
56-
.registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())
57-
.create();
58-
String payloadJson = gson.toJson(payload);
59-
System.out.println("gosn.toJSON: " + payloadJson);
60-
PushPayload newPayload = gson.fromJson(payloadJson, PushPayload.class);
61-
System.out.println("newPayload:" + newPayload.toString());
6255
try {
63-
PushResult result = jpushClient.sendPush(newPayload);
56+
PushResult result = jpushClient.sendPush(payload);
6457
LOG.info("Got result - " + result);
6558

6659
} catch (APIConnectionException e) {
@@ -75,15 +68,16 @@ public static void testSendPush() {
7568
}
7669
}
7770

78-
//use Payload.fromJSON to build payload instance
71+
//use String to build PushPayload instance
7972
public static void testSendPush_fromJSON() {
8073
ClientConfig clientConfig = ClientConfig.getInstance();
8174
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
82-
83-
String payloadString = "{\"platform\":{\"all\":\"false\",\"deviceTypes\":[\"android\",\"ios\"]},\"audience\":{\"all\":\"true\"},\"notification\":{\"alert\":\"Test\",\"ios\":{\"alert\":\"第 1 条\",\"extras\":{\"extra_key\":\"extra_value\"},\"badge\":\"+1\",\"sound\":\"\"},\"android\":{\"alert\":\"第 1 条\",\"extras\":{\"android_key\":\"android_value\"}}},\"options\":{\"sendno\":182145298,\"apns_production\":false}}";
84-
//also equals
85-
//String payloadString = "{\"platform\":{\"all\":\"true\"},\"audience\":{\"all\":\"true\"},\"notification\":{\"alert\":\"Test\",\"ios\":{\"alert\":\"第 1 条\",\"extras\":{\"extra_key\":\"extra_value\"},\"badge\":\"+1\",\"sound\":\"\"},\"android\":{\"alert\":\"第 1 条\",\"extras\":{\"android_key\":\"android_value\"}}},\"options\":{\"sendno\":182145298,\"apns_production\":false}}";
86-
PushPayload payload = PushPayload.fromJSON(payloadString);
75+
Gson gson = new GsonBuilder()
76+
.registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())
77+
.create();
78+
// Since the type of DeviceType is enum, thus the value should be uppercase, same with the AudienceType.
79+
String payloadString = "{\"platform\":{\"all\":false,\"deviceTypes\":[\"IOS\"]},\"audience\":{\"all\":false,\"targets\":[{\"audienceType\":\"TAG_AND\",\"values\":[\"tag1\",\"tag_all\"]}]},\"notification\":{\"notifications\":[{\"soundDisabled\":false,\"badgeDisabled\":false,\"sound\":\"happy\",\"badge\":\"5\",\"contentAvailable\":false,\"alert\":\"Test from API Example - alert\",\"extras\":{\"from\":\"JPush\"},\"type\":\"cn.jpush.api.push.model.notification.IosNotification\"}]},\"message\":{\"msgContent\":\"Test from API Example - msgContent\"},\"options\":{\"sendno\":1429488213,\"overrideMsgId\":0,\"timeToLive\":-1,\"apnsProduction\":true,\"bigPushDuration\":0}}";
80+
PushPayload payload = gson.fromJson(payloadString, PushPayload.class);
8781
try {
8882
PushResult result = jpushClient.sendPush(payload);
8983
LOG.info("Got result - " + result);

0 commit comments

Comments
 (0)