Skip to content

Commit 5fa58da

Browse files
committed
add SMS example and bind mobile api
1 parent 22a09ef commit 5fa58da

File tree

7 files changed

+110
-4
lines changed

7 files changed

+110
-4
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,26 @@
200200
}
201201
```
202202

203+
* 构建推送对象:推送内容包含SMS信息
204+
205+
```Java
206+
public static void testSendWithSMS() {
207+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
208+
try {
209+
SMS sms = SMS.content("Test SMS", 10);
210+
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
211+
LOG.info("Got result - " + result);
212+
} catch (APIConnectionException e) {
213+
LOG.error("Connection error. Should retry later. ", e);
214+
} catch (APIRequestException e) {
215+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
216+
LOG.info("HTTP Status: " + e.getStatus());
217+
LOG.info("Error Code: " + e.getErrorCode());
218+
LOG.info("Error Message: " + e.getErrorMessage());
219+
}
220+
}
221+
```
222+
203223
### 统计获取样例
204224

205225
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ReportsExample
@@ -227,6 +247,7 @@
227247

228248
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.DeviceExample
229249
250+
* 获取Tag Alias
230251
```Java
231252
try {
232253
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
@@ -243,6 +264,22 @@
243264
}
244265
```
245266

267+
* 绑定手机号
268+
269+
```Java
270+
try {
271+
DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
272+
LOG.info("Got result " + result);
273+
} catch (APIConnectionException e) {
274+
LOG.error("Connection error. Should retry later. ", e);
275+
} catch (APIRequestException e) {
276+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
277+
LOG.info("HTTP Status: " + e.getStatus());
278+
LOG.info("Error Code: " + e.getErrorCode());
279+
LOG.info("Error Message: " + e.getErrorMessage());
280+
}
281+
```
282+
246283
### Schedule 样例
247284

248285
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ScheduleExample

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.examples;
22

3+
import cn.jpush.api.common.resp.DefaultResult;
34
import cn.jpush.api.device.OnlineStatus;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -62,6 +63,20 @@ public static void testGetUserOnlineStatus() {
6263
LOG.info("Error Message: " + e.getErrorMessage());
6364
}
6465
}
66+
67+
public static void testBindMobile() {
68+
try {
69+
DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
70+
LOG.info("Got result " + result);
71+
} catch (APIConnectionException e) {
72+
LOG.error("Connection error. Should retry later. ", e);
73+
} catch (APIRequestException e) {
74+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
75+
LOG.info("HTTP Status: " + e.getStatus());
76+
LOG.info("Error Code: " + e.getErrorCode());
77+
LOG.info("Error Message: " + e.getErrorMessage());
78+
}
79+
}
6580

6681
}
6782

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.jpush.api.examples;
22

33
import cn.jpush.api.common.ClientConfig;
4+
import cn.jpush.api.push.model.*;
45
import cn.jpush.api.push.model.notification.IosAlert;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
@@ -9,10 +10,6 @@
910
import cn.jpush.api.common.resp.APIConnectionException;
1011
import cn.jpush.api.common.resp.APIRequestException;
1112
import cn.jpush.api.push.PushResult;
12-
import cn.jpush.api.push.model.Message;
13-
import cn.jpush.api.push.model.Options;
14-
import cn.jpush.api.push.model.Platform;
15-
import cn.jpush.api.push.model.PushPayload;
1613
import cn.jpush.api.push.model.audience.Audience;
1714
import cn.jpush.api.push.model.audience.AudienceTarget;
1815
import cn.jpush.api.push.model.notification.AndroidNotification;
@@ -178,5 +175,21 @@ public static void testSendIosAlert() {
178175
}
179176
}
180177

178+
public static void testSendWithSMS() {
179+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
180+
try {
181+
SMS sms = SMS.content("Test SMS", 10);
182+
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
183+
LOG.info("Got result - " + result);
184+
} catch (APIConnectionException e) {
185+
LOG.error("Connection error. Should retry later. ", e);
186+
} catch (APIRequestException e) {
187+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
188+
LOG.info("HTTP Status: " + e.getStatus());
189+
LOG.info("Error Code: " + e.getErrorCode());
190+
LOG.info("Error Message: " + e.getErrorMessage());
191+
}
192+
}
193+
181194
}
182195

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,12 @@ public Map<String, OnlineStatus> getUserOnlineStatus(String... registrationIds)
858858
return _deviceClient.getUserOnlineStatus(registrationIds);
859859
}
860860

861+
public DefaultResult bindMobile(String registrationId, String mobile)
862+
throws APIConnectionException, APIRequestException
863+
{
864+
return _deviceClient.bindMobile(registrationId, mobile);
865+
}
866+
861867
// ----------------------- Schedule
862868

863869
/**

src/main/java/cn/jpush/api/device/DeviceClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import cn.jpush.api.common.connection.NativeHttpClient;
88
import cn.jpush.api.common.resp.*;
99
import cn.jpush.api.utils.Preconditions;
10+
import cn.jpush.api.utils.StringUtils;
1011
import com.google.gson.Gson;
1112
import com.google.gson.JsonArray;
1213
import com.google.gson.JsonObject;
@@ -114,6 +115,20 @@ public DefaultResult updateDeviceTagAlias(String registrationId, String alias,
114115
return DefaultResult.fromResponse(response);
115116
}
116117

118+
public DefaultResult bindMobile(String registrationId, String mobile)
119+
throws APIConnectionException, APIRequestException
120+
{
121+
Preconditions.checkArgument(StringUtils.isMobileNumber(mobile), "The mobile format is incorrect. " + mobile);
122+
123+
String url = hostName + devicesPath + "/" + registrationId;
124+
JsonObject top = new JsonObject();
125+
if (null != mobile) {
126+
top.addProperty("mobile", mobile);
127+
}
128+
ResponseWrapper response = _httpClient.sendPost(url, top.toString());
129+
return DefaultResult.fromResponse(response);
130+
}
131+
117132
// ------------- tags
118133

119134
public TagListResult getTagList() throws APIConnectionException, APIRequestException {

src/main/java/cn/jpush/api/utils/StringUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import java.io.UnsupportedEncodingException;
44
import java.net.URLEncoder;
55
import java.security.MessageDigest;
6+
import java.util.regex.Pattern;
67

78

89
public class StringUtils {
910
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
1011
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
1112

13+
private static Pattern pattern = Pattern.compile("^(1[34578][0-9])(\\d{4})(\\d{4})$");
14+
1215
private static String byteArrayToHexString(byte[] b) {
1316
StringBuffer resultSb = new StringBuffer();
1417
for (int i = 0; i < b.length; i++) {
@@ -86,4 +89,14 @@ public static boolean isLineBroken(String s) {
8689
return false;
8790
}
8891

92+
public static boolean isMobileNumber(String s) {
93+
if (null == s) {
94+
return false;
95+
}
96+
if (pattern.matcher(s).matches()) {
97+
return true;
98+
}
99+
return false;
100+
}
101+
89102
}

src/test/java/cn/jpush/api/device/DeviceNormalRemoteTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@ public void testTetUserOnlineStatus() throws APIConnectionException, APIRequestE
174174
Map<String, OnlineStatus> result = jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2);
175175
assertTrue(result.get(REGISTRATION_ID1) != null);
176176
}
177+
178+
@Test
179+
@TestOrder(order = 360)
180+
public void testBindMobile() throws APIConnectionException, APIRequestException {
181+
DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
182+
assertTrue(result.isResultOK());
183+
}
177184
}

0 commit comments

Comments
 (0)