Skip to content

Commit 1b6fb54

Browse files
author
Javen
committed
Add more example code in README.md;
Add shortcut methods for Platform.
1 parent 9720e51 commit 1b6fb54

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

README.md

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
对应的 REST API 文档:<http://docs.jpush.cn/display/dev/REST+API>
77

8+
API Javadoc:[API Docs](http://jpush.github.io/jpush-api-java-client/apidocs/)
9+
810
## 安装
911

1012
### maven 方式(3.0.0 版本 maven 库里还没有,请直接用 jar 的方式)
@@ -22,8 +24,9 @@
2224
请到 [Release页面](https://github.com/jpush/jpush-api-java-client/releases)下载相应版本的发布包。
2325

2426
### 依赖包
25-
* gson
26-
* slf4j
27+
* slf4j/log4j (Log)
28+
* gson (Google)
29+
* guava (Google)
2730

2831
> 其中 slf4j 可以与 logback, log4j, commons-logging 等日志框架一起工作,可根据你的需要配置使用。
2932
@@ -80,14 +83,14 @@ maven package
8083
```
8184

8285
## 使用样例
83-
下边是简单直接的使用样例。
84-
详细地了解请参考:[API Docs](http://jpush.github.io/jpush-api-java-client/apidocs/)
8586

8687
### 推送样例
8788

89+
> 以下片断来自项目代码里的文件:cn.jpush.api.examples.PushExample
90+
8891
```
8992
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
90-
PushPayload payload = PushPayload.alertAll("Hi, JPush!");
93+
PushPayload payload = buildPushObject_all_all_alert();
9194
LOG.info("Paylaod JSON - " + payload.toString());
9295
9396
PushResult result = jpushClient.sendPush(payload);
@@ -103,8 +106,79 @@ maven package
103106
104107
```
105108

109+
进行推送的关键在于构建一个 PushPayload 对象。对下示例一般的构建对象的用法。
110+
111+
* 快捷地构建推送对象:所有平台,所有设备,内容为 ALERT 的通知。
112+
```
113+
public static PushPayload buildPushObject_all_all_alert() {
114+
return PushPayload.alertAll(ALERT);
115+
}
116+
```
117+
118+
* 构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT。
119+
```
120+
public static PushPayload buildPushObject_all_alias_alert() {
121+
return PushPayload.newBuilder()
122+
.setPlatform(Platform.all())
123+
.setAudience(Audience.alias("alias1"))
124+
.setNotification(Notification.alert(ALERT))
125+
.build();
126+
}
127+
```
128+
129+
* 构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE。
130+
```
131+
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
132+
return PushPayload.newBuilder()
133+
.setPlatform(Platform.android())
134+
.setAudience(Audience.tag("tag1"))
135+
.setNotification(Notification.newBuilder()
136+
.addPlatformNotification(AndroidNotification.newBuilder()
137+
.setAlert(ALERT)
138+
.setTitle(TITLE)
139+
.build())
140+
.build())
141+
.build();
142+
}
143+
```
144+
145+
* 构建推送对象:平台是 iOS,推送目标是 "tag1", "tag_all" 的并集,推送内容同时包括通知与消息 - 通知信息是 ALERT,并且附加字段 from = "JPush";消息内容是 MSG_CONTENT。通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。
146+
```
147+
public static PushPayload buildPushObject_ios_tagAnd_alertWithExtras() {
148+
return PushPayload.newBuilder()
149+
.setPlatform(Platform.ios())
150+
.setAudience(Audience.tag_and("tag1", "tag_all"))
151+
.setNotification(Notification.newBuilder()
152+
.addPlatformNotification(IosNotification.newBuilder()
153+
.setAlert(ALERT)
154+
.addExtra("from", "JPush")
155+
.build())
156+
.build())
157+
.build();
158+
}
159+
```
160+
161+
* 构建推送对象:平台是 Andorid 与 iOS,推送目标是 ("tag1" 与 "tag2" 的交集)并("alias1" 与 "alias2" 的交集),推送内容是 - 内容为 MSG_CONTENT 的消息,并且附加字段 from = JPush。
162+
```
163+
public static PushPayload buildPushObject_ios_audienceMore_message() {
164+
return PushPayload.newBuilder()
165+
.setPlatform(Platform.android_ios())
166+
.setAudience(Audience.newBuilder()
167+
.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
168+
.addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
169+
.build())
170+
.setMessage(Message.newBuilder()
171+
.setMsgContent(MSG_CONTENT)
172+
.addExtra("from", "JPush")
173+
.build())
174+
.build();
175+
}
176+
```
177+
106178
### 统计获取样例
107179

180+
> 以下片断来自项目代码里的文件:cn.jpush.api.examples.ReportsExample
181+
108182
```
109183
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
110184
ReceivedsResult receivedsResult = jpushClient.getReportReceiveds("1708010723,1774452771");

src/cn/jpush/api/examples/PushExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtras() {
8686
.addExtra("news_id", 333)
8787
.build())
8888
.build())
89+
.setMessage(Message.content(MSG_CONTENT))
8990
.build();
9091
}
9192

9293
public static PushPayload buildPushObject_ios_audienceMore_message() {
9394
return PushPayload.newBuilder()
94-
.setPlatform(Platform.ios())
95+
.setPlatform(Platform.android_ios())
9596
.setAudience(Audience.newBuilder()
9697
.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
9798
.addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))

src/cn/jpush/api/push/model/Platform.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ public static Platform winphone() {
3939
return newBuilder().addDeviceType(DeviceType.WinPhone).build();
4040
}
4141

42+
public static Platform android_ios() {
43+
return newBuilder()
44+
.addDeviceType(DeviceType.Android)
45+
.addDeviceType(DeviceType.IOS)
46+
.build();
47+
}
48+
49+
public static Platform android_winphone() {
50+
return newBuilder()
51+
.addDeviceType(DeviceType.Android)
52+
.addDeviceType(DeviceType.WinPhone)
53+
.build();
54+
}
55+
56+
public static Platform ios_winphone() {
57+
return newBuilder()
58+
.addDeviceType(DeviceType.IOS)
59+
.addDeviceType(DeviceType.WinPhone)
60+
.build();
61+
}
62+
4263
public boolean isAll() {
4364
return all;
4465
}

0 commit comments

Comments
 (0)