|
| 1 | +package cn.jpush.api.push; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import cn.jpush.api.JPushClient; |
| 9 | +import cn.jpush.api.push.model.Platform; |
| 10 | +import cn.jpush.api.push.model.PushPayload; |
| 11 | +import cn.jpush.api.push.model.audience.Audience; |
| 12 | +import cn.jpush.api.push.model.audience.AudienceTarget; |
| 13 | +import cn.jpush.api.push.model.audience.AudienceType; |
| 14 | +import cn.jpush.api.push.model.notification.Notification; |
| 15 | + |
| 16 | +public class AudienceTests { |
| 17 | + private static final String appKey ="dd1066407b044738b6479275"; |
| 18 | + private static final String masterSecret = "2b38ce69b1de2a7fa95706ea"; |
| 19 | + |
| 20 | + private static final int SUCCEED_RESULT_CODE = 0; |
| 21 | + private static final String TAG = "tag1"; |
| 22 | + private static final String TAG2 = "tag2"; |
| 23 | + private static final String TAG3 = "tag3"; |
| 24 | + private static final String ALIAS = "alias1"; |
| 25 | + private static final String ALIAS2 = "alias2"; |
| 26 | + private static final String ALIAS3 = "alias3"; |
| 27 | + private static final String REGISTRATION = "0900e8d85ef"; |
| 28 | + private static final String REGISTRATION2 = "0900e8d85ef"; |
| 29 | + private static final String ALERT = "JPush Test - alert"; |
| 30 | + private static final String MSG_CONTENT = "JPush Test - msgContent"; |
| 31 | + |
| 32 | + private JPushClient _client = null; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void before() { |
| 36 | + _client = new JPushClient(masterSecret, appKey); |
| 37 | + } |
| 38 | + |
| 39 | + // one -------- |
| 40 | + |
| 41 | + @Test |
| 42 | + public void sendByTag() { |
| 43 | + PushPayload payload = PushPayload.newBuilder() |
| 44 | + .setPlatform(Platform.all()) |
| 45 | + .setAudience(Audience.tag(TAG)) |
| 46 | + .setNotification(Notification.alert(ALERT)) |
| 47 | + .build(); |
| 48 | + PushResult result = _client.sendPush(payload); |
| 49 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void sendByTagAnd() { |
| 54 | + PushPayload payload = PushPayload.newBuilder() |
| 55 | + .setPlatform(Platform.all()) |
| 56 | + .setAudience(Audience.tag_and(TAG)) |
| 57 | + .setNotification(Notification.alert(ALERT)) |
| 58 | + .build(); |
| 59 | + PushResult result = _client.sendPush(payload); |
| 60 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void sendByAlias() { |
| 65 | + PushPayload payload = PushPayload.newBuilder() |
| 66 | + .setPlatform(Platform.all()) |
| 67 | + .setAudience(Audience.alias(ALIAS)) |
| 68 | + .setNotification(Notification.alert(ALERT)) |
| 69 | + .build(); |
| 70 | + PushResult result = _client.sendPush(payload); |
| 71 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void sendByRegistrationID() { |
| 76 | + PushPayload payload = PushPayload.newBuilder() |
| 77 | + .setPlatform(Platform.all()) |
| 78 | + .setAudience(Audience.registrationId(REGISTRATION)) |
| 79 | + .setNotification(Notification.alert(ALERT)) |
| 80 | + .build(); |
| 81 | + PushResult result = _client.sendPush(payload); |
| 82 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 83 | + } |
| 84 | + |
| 85 | + // one more ------------------------- |
| 86 | + |
| 87 | + @Test |
| 88 | + public void sendByTagMore() { |
| 89 | + PushPayload payload = PushPayload.newBuilder() |
| 90 | + .setPlatform(Platform.all()) |
| 91 | + .setAudience(Audience.tag(TAG, TAG2)) |
| 92 | + .setNotification(Notification.alert(ALERT)) |
| 93 | + .build(); |
| 94 | + PushResult result = _client.sendPush(payload); |
| 95 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void sendByTagAndMore() { |
| 100 | + PushPayload payload = PushPayload.newBuilder() |
| 101 | + .setPlatform(Platform.all()) |
| 102 | + .setAudience(Audience.tag_and(TAG, TAG3)) |
| 103 | + .setNotification(Notification.alert(ALERT)) |
| 104 | + .build(); |
| 105 | + PushResult result = _client.sendPush(payload); |
| 106 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void sendByAliasMore() { |
| 111 | + PushPayload payload = PushPayload.newBuilder() |
| 112 | + .setPlatform(Platform.all()) |
| 113 | + .setAudience(Audience.alias(ALIAS, ALIAS2)) |
| 114 | + .setNotification(Notification.alert(ALERT)) |
| 115 | + .build(); |
| 116 | + PushResult result = _client.sendPush(payload); |
| 117 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + @Test |
| 122 | + public void sendByRegistrationIDMore() { |
| 123 | + PushPayload payload = PushPayload.newBuilder() |
| 124 | + .setPlatform(Platform.all()) |
| 125 | + .setAudience(Audience.registrationId(REGISTRATION, REGISTRATION2)) |
| 126 | + .setNotification(Notification.alert(ALERT)) |
| 127 | + .build(); |
| 128 | + PushResult result = _client.sendPush(payload); |
| 129 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + // composite ------------------------- |
| 135 | + |
| 136 | + @Test |
| 137 | + public void sendByTagAlias() { |
| 138 | + PushPayload payload = PushPayload.newBuilder() |
| 139 | + .setPlatform(Platform.all()) |
| 140 | + .setAudience(Audience.newBuilder() |
| 141 | + .addAudienceTarget(AudienceTarget.newBuilder() |
| 142 | + .setAudienceType(AudienceType.ALIAS) |
| 143 | + .addAudienceTargetValue(ALIAS).build()) |
| 144 | + .addAudienceTarget(AudienceTarget.newBuilder() |
| 145 | + .setAudienceType(AudienceType.TAG) |
| 146 | + .addAudienceTargetValue(TAG).build()) |
| 147 | + .build()) |
| 148 | + .setNotification(Notification.alert(ALERT)) |
| 149 | + .build(); |
| 150 | + PushResult result = _client.sendPush(payload); |
| 151 | + assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode()); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | +} |
| 156 | + |
0 commit comments