|
| 1 | +package cn.jpush.api.examples; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import cn.jpush.api.JPushClient; |
| 12 | +import cn.jpush.api.common.resp.APIConnectionException; |
| 13 | +import cn.jpush.api.common.resp.APIRequestException; |
| 14 | +import cn.jpush.api.common.resp.BooleanResult; |
| 15 | +import cn.jpush.api.common.resp.DefaultResult; |
| 16 | +import cn.jpush.api.device.AliasDeviceListResult; |
| 17 | +import cn.jpush.api.device.TagAliasResult; |
| 18 | +import cn.jpush.api.device.TagListResult; |
| 19 | + |
| 20 | +public class DevcieExample { |
| 21 | + protected static final Logger LOG = LoggerFactory.getLogger(DevcieExample.class); |
| 22 | + |
| 23 | + private static final String appKey = "dd1066407b044738b6479275"; |
| 24 | + private static final String masterSecret = "2b38ce69b1de2a7fa95706ea"; |
| 25 | + private static JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3); |
| 26 | + private static final String TAG1 = "tag1"; |
| 27 | + private static final String ALIAS1 = "alias1"; |
| 28 | + private static final String ALIAS2 = "alias2"; |
| 29 | + private static final String REGISTRATION_ID1 = "0900e8d85ef"; |
| 30 | + private static final String REGISTRATION_ID2 = "0a04ad7d8b4"; |
| 31 | + |
| 32 | + |
| 33 | + public static void main(String[] args) throws Exception { |
| 34 | + testGetDeviceTagAlias(); |
| 35 | + testUpdateDeviceTagAlias(); |
| 36 | + testGetTagList(); |
| 37 | + testIsDeviceInTag(); |
| 38 | + testAddRemoveDevicesFromTag(); |
| 39 | + testDeleteTag(); |
| 40 | + testGetAliasDeviceList(); |
| 41 | + testDeleteAlias(); |
| 42 | + } |
| 43 | + public static void testGetDeviceTagAlias() throws Exception { |
| 44 | + TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1); |
| 45 | + assertTrue(result.isResultOK()); |
| 46 | + } |
| 47 | + |
| 48 | + public static void testUpdateDeviceTagAlias() |
| 49 | + throws APIConnectionException, APIRequestException { |
| 50 | + Set<String> tagsToAdd = new HashSet<String>(); |
| 51 | + tagsToAdd.add("tag2"); |
| 52 | + tagsToAdd.add("tag2"); |
| 53 | + Set<String> tagsToRemove = new HashSet<String>(); |
| 54 | + tagsToRemove.add("tag4"); |
| 55 | + tagsToRemove.add("tag3"); |
| 56 | + DefaultResult result = jpushClient.updateDeviceTagAlias( |
| 57 | + REGISTRATION_ID1, ALIAS1, false, tagsToAdd, tagsToRemove); |
| 58 | + assertTrue(result.isResultOK()); |
| 59 | + } |
| 60 | + |
| 61 | + public static void testGetTagList() throws APIConnectionException, |
| 62 | + APIRequestException { |
| 63 | + TagListResult result = jpushClient.getTagList(); |
| 64 | + assertTrue(result.isResultOK()); |
| 65 | + } |
| 66 | + |
| 67 | + public static void testIsDeviceInTag() throws APIConnectionException, |
| 68 | + APIRequestException { |
| 69 | + BooleanResult result = jpushClient |
| 70 | + .isDeviceInTag(TAG1, REGISTRATION_ID1); |
| 71 | + assertTrue(result.isResultOK()); |
| 72 | + } |
| 73 | + |
| 74 | + public static void testAddRemoveDevicesFromTag() |
| 75 | + throws APIConnectionException, APIRequestException { |
| 76 | + Set<String> toAddUsers = new HashSet<String>(); |
| 77 | + toAddUsers.add(REGISTRATION_ID1); |
| 78 | + Set<String> toRemoveUsers = new HashSet<String>(); |
| 79 | + toRemoveUsers.add(REGISTRATION_ID2); |
| 80 | + DefaultResult result = jpushClient.addRemoveDevicesFromTag(TAG1, |
| 81 | + toAddUsers, toRemoveUsers); |
| 82 | + assertTrue(result.isResultOK()); |
| 83 | + } |
| 84 | + |
| 85 | + public static void testDeleteTag() throws APIConnectionException, |
| 86 | + APIRequestException { |
| 87 | + DefaultResult result = jpushClient.deleteTag(TAG1, null); |
| 88 | + assertTrue(result.isResultOK()); |
| 89 | + } |
| 90 | + |
| 91 | + public static void testGetAliasDeviceList() throws APIConnectionException, |
| 92 | + APIRequestException { |
| 93 | + AliasDeviceListResult result = jpushClient.getAliasDeviceList(ALIAS1, |
| 94 | + null); |
| 95 | + assertTrue(result.isResultOK()); |
| 96 | + } |
| 97 | + |
| 98 | + public static void testDeleteAlias() throws APIConnectionException, |
| 99 | + APIRequestException { |
| 100 | + DefaultResult result = jpushClient.deleteAlias(ALIAS2, null); |
| 101 | + assertTrue(result.isResultOK()); |
| 102 | + } |
| 103 | +} |
0 commit comments