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 的方式)
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```
109183JPushClient jpushClient = new JPushClient(masterSecret, appKey);
110184ReceivedsResult receivedsResult = jpushClient.getReportReceiveds("1708010723,1774452771");
0 commit comments