Skip to content

Commit 49c952d

Browse files
committed
ApiBoot Message Push 示例
1 parent 0b336b2 commit 49c952d

File tree

4 files changed

+400
-0
lines changed

4 files changed

+400
-0
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
## ApiBoot Message Push
2+
3+
`消息推送`是接口服务项目内不可或缺的一部分,用于向用户发送操作消息提醒、广告等。
4+
5+
`ApiBoot`内提供了`消息推送`的支持,目前集成的第三方组件为`极光推送``ApiBoot`遵循开箱即用的原则,所以在集成推送时仍然只需要简单的必要配置即可完成集成。
6+
7+
### 添加依赖
8+
9+
`pom.xml`配置文件添加依赖如下所示:
10+
11+
```xml
12+
//...
13+
<dependencies>
14+
<!--ApiBoot Message Push Starter-->
15+
<dependency>
16+
<groupId>org.minbox.framework</groupId>
17+
<artifactId>api-boot-starter-message-push</artifactId>
18+
</dependency>
19+
</dependencies>
20+
21+
<dependencyManagement>
22+
<dependencies>
23+
<!--ApiBoot 版本依赖-->
24+
<dependency>
25+
<groupId>org.minbox.framework</groupId>
26+
<artifactId>api-boot-dependencies</artifactId>
27+
<version>2.0.5.RELEASE</version>
28+
<scope>import</scope>
29+
<type>pom</type>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
//...
34+
```
35+
36+
37+
38+
### 相关配置
39+
40+
| 配置名称 | 默认值 | 描述 |
41+
| ------------------------------------------ | ------ | -------------------------------------------------------- |
42+
| `api.boot.push.production` | `true` | 配置IOS平台的推送环境,true:生产环境(AppStore下载的APP) |
43+
| `api.boot.push.client.master-secret` | | 单客户端推送配置masterSecret |
44+
| `api.boot.push.client.app-key` | | 单客户端推送配置appKey |
45+
| `api.boot.push.multiple.xxx.master-secret` | | 多客户端推送配置masterSecret |
46+
| `api.boot.push.multiple.xxx.app-key` | | 多客户端推送配置appKey |
47+
48+
> 在上面`multiple`相关的配置内部是key->value结构配置,其中"xxx"用于配置@MessagePushSwitch("xxx")的value值
49+
50+
### 单环境推送
51+
52+
如果你的接口服务项目只集成推送到一个APP客户端,那么可以采用`单环境推送`的模式,该环境使用`api.boot.push.client`前缀的配置进行配置极光推送的两个秘钥,如下所示:
53+
54+
```yaml
55+
# ApiBoot Config
56+
api:
57+
boot:
58+
push:
59+
# 推送默认配置
60+
client:
61+
# 秘钥
62+
master-secret: xxx
63+
# appKey
64+
app-key: xxx
65+
```
66+
67+
配置完成后我们就可以通过注入`ApiBootMessagePushService`接口来进行发送推送业务,如下所示:
68+
69+
```java
70+
/**
71+
* ApiBoot Message Push Service
72+
*/
73+
@Autowired
74+
private ApiBootMessagePushService apiBootMessagePushService;
75+
76+
/**
77+
* 安卓推送示例
78+
* @see org.minbox.framework.api.boot.plugin.message.push.model.PushClientConfig
79+
*/
80+
public void test() {
81+
apiBootMessagePushService.executePush(
82+
MessagePushBody.builder()
83+
// 推送安卓平台
84+
// 如需更换推送平台,可以查看PusherPlatform枚举定义
85+
.platform(PusherPlatform.ANDROID)
86+
// 标题
87+
.title("消息推送")
88+
// 内容
89+
.message("测试消息推送内容")
90+
// 接收人的别名(App端开发人员进行设置)
91+
.alias(Arrays.asList("xxxx"))
92+
.build()
93+
);
94+
}
95+
```
96+
97+
> `ApiBoot Message Push`如不配置使用`@MessagePushSwithc`注解时,使用默认的配置,也就是`api.boot.push.client`开头的相关配置信息。
98+
99+
### 多环境推送
100+
101+
在一个接口服务系统内,可能存在向多个APP客户端进行推送消息,那么这种情况`ApiBoot Message Push`也做了支持,内部通过`AOP`的方法切面进行处理。
102+
103+
多环境下的配置要做修改,如下所示:
104+
105+
```yaml
106+
# ApiBoot Config
107+
api:
108+
boot:
109+
push:
110+
multiple:
111+
# user app 推送配置
112+
user:
113+
master-secret: xxx
114+
app-key: xxxx
115+
# other app 推送配置
116+
other:
117+
master-secret: xxxxxx
118+
app-key: xxxxxx
119+
```
120+
121+
在上面我们配置了两个APP客户端的推送信息,名称分别为`user`、`other`,这个名称对应`@MessagePushSwitch`注解的`value`值使用。
122+
123+
在上面配置了`user`、`other`两个客户端,使用具体的环境我们只需要在调用方法上添加`@MessagePushSwitch`注解,如下所示:
124+
125+
```java
126+
// other app 推送
127+
@MessagePushSwitch("other")
128+
public void testOther() {
129+
//....
130+
}
131+
132+
// user app 推送
133+
@MessagePushSwitch("user")
134+
public void testUser() {
135+
//....
136+
}
137+
```
138+
139+
140+
141+
### 指定别名推送
142+
143+
在推送消息时,`ApiBoot`提供了多种方式,其中可以根据`alias`别名进行推送,`alias`是由`App`前端开发人员在集成`SDK`时设置的值,这时就需要接口开发人员与APP开发人员的约定来完成推送逻辑,一般可以使用用户的编号来作为别名,根据别名推送如下所示:
144+
145+
```java
146+
// 别名推送示例
147+
public void test() {
148+
apiBootMessagePushService.executePush(
149+
MessagePushBody.builder()
150+
.platform(PusherPlatform.ANDROID)
151+
.title("消息推送")
152+
.message("测试消息推送内容")
153+
// 配置别名,别名可配置多个
154+
.alias(Arrays.asList("xxxx"))
155+
.build()
156+
);
157+
}
158+
```
159+
160+
161+
162+
### 指定分组推送
163+
164+
分组推送跟别名推送几乎一致,也是APP开发人员在调用`SDK`时对具体的用户划分的一个分组群体,向分组进行推送消息时,分组内的所有用户都可以收到。
165+
166+
如下所示:
167+
168+
```java
169+
// 分组推送示例
170+
public void test() {
171+
apiBootMessagePushService.executePush(
172+
MessagePushBody.builder()
173+
.platform(PusherPlatform.ANDROID)
174+
.title("消息推送")
175+
.message("测试消息推送内容")
176+
// 推送group1内的所有用户
177+
// 注意:分组优先级低于别名
178+
.tags(Arrays.asList("group1"))
179+
.build()
180+
);
181+
}
182+
```
183+
184+
> 如果同时配置别名、分组两种推送方式,则会使用别名
185+
186+
### 携带自定义参数推送
187+
188+
`ApiBoot Message Push`可以给APP端推送的消息携带自定义的扩展参数,让APP端做一些特殊的处理,携带扩展参数如下所示:
189+
190+
```java
191+
// 自定义参数示例
192+
public void test() {
193+
apiBootMessagePushService.executePush(
194+
MessagePushBody.builder()
195+
.platform(PusherPlatform.ANDROID)
196+
.title("消息推送")
197+
.message("测试消息推送内容")
198+
.alias(Arrays.asList("xxxx"))
199+
// 扩展参数
200+
.extras(new HashMap(1) {
201+
{
202+
put("role", "USER");
203+
}
204+
})
205+
.build()
206+
);
207+
}
208+
```
209+
210+
211+
212+
### 自定义推送提示声音
213+
214+
`ApiBoot Message Push`可以推送自定义提示的声音,当然也需要APP开发人员做配置才可以,如下所示:
215+
216+
```java
217+
public void test() {
218+
apiBootMessagePushService.executePush(
219+
MessagePushBody.builder()
220+
.platform(PusherPlatform.ANDROID)
221+
.title("消息推送")
222+
.message("测试消息推送内容")
223+
.alias(Arrays.asList("xxxx"))
224+
// 手机系统默认提示音
225+
.sound("default")
226+
.build()
227+
);
228+
}
229+
```
230+
231+
`sound`方法可以设置与`APP`开发人员约定的声明唯一名称。
232+
233+
### 设置推送角标数值
234+
235+
推送消息时`APP`的角标数值是可以自定义的,当然我们一般都是叠加,如下所示:
236+
237+
```java
238+
public void test() {
239+
apiBootMessagePushService.executePush(
240+
MessagePushBody.builder()
241+
.platform(PusherPlatform.ANDROID)
242+
.title("消息推送")
243+
.message("测试消息推送内容")
244+
.alias(Arrays.asList("xxxx"))
245+
.tags(Arrays.asList("group1"))
246+
// 角标数值,默认为+1
247+
.badge(999)
248+
.build()
249+
);
250+
}
251+
```
252+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright [2019] [恒宇少年 - 于起宇]
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<artifactId>api-boot-samples</artifactId>
24+
<groupId>org.minbox.framework</groupId>
25+
<version>2.0.5.RELEASE</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<artifactId>api-boot-sample-message-push</artifactId>
30+
<description>
31+
ApiBoot Message Push
32+
示例
33+
</description>
34+
35+
<dependencies>
36+
<!--ApiBoot Message Push Starter-->
37+
<dependency>
38+
<groupId>org.minbox.framework</groupId>
39+
<artifactId>api-boot-starter-message-push</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
<dependencyManagement>
44+
<dependencies>
45+
<!--ApiBoot 版本依赖-->
46+
<dependency>
47+
<groupId>org.minbox.framework</groupId>
48+
<artifactId>api-boot-dependencies</artifactId>
49+
<version>2.0.5.RELEASE</version>
50+
<scope>import</scope>
51+
<type>pom</type>
52+
</dependency>
53+
</dependencies>
54+
</dependencyManagement>
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.sample.message.push;
19+
20+
import org.minbox.framework.api.boot.plugin.message.push.ApiBootMessagePushService;
21+
import org.minbox.framework.api.boot.plugin.message.push.annotation.MessagePushSwitch;
22+
import org.minbox.framework.api.boot.plugin.message.push.model.MessagePushBody;
23+
import org.minbox.framework.api.boot.plugin.message.push.model.PusherPlatform;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.stereotype.Service;
26+
27+
import java.util.Arrays;
28+
29+
/**
30+
* @author:恒宇少年 - 于起宇
31+
* <p>
32+
* DateTime:2019-04-22 14:46
33+
* Blog:http://blog.yuqiyu.com
34+
* WebSite:http://www.jianshu.com/u/092df3f77bca
35+
* Gitee:https://gitee.com/hengboy
36+
* GitHub:https://github.com/hengboy
37+
*/
38+
@Service
39+
public class MessagePushSampleService {
40+
/**
41+
* ApiBoot Message Push Service
42+
*/
43+
@Autowired
44+
private ApiBootMessagePushService apiBootMessagePushService;
45+
46+
/**
47+
* 注解 @MessagePushSwitch 不添加时使用默认配置
48+
* 注解 @MessagePushSwitch 添加时不配置value使用默认配置
49+
* 注解 @MessagePushSwitch 添加时配置value,则使用配置的value配置
50+
* 对应api.boot.push.multiple的key
51+
*
52+
* @see org.minbox.framework.api.boot.plugin.message.push.model.PushClientConfig
53+
*/
54+
//@MessagePushSwitch
55+
//@MessagePushSwitch("user")
56+
@MessagePushSwitch("other")
57+
public void test() {
58+
apiBootMessagePushService.executePush(
59+
MessagePushBody.builder()
60+
.platform(PusherPlatform.ANDROID)
61+
.title("消息推送")
62+
.message("测试消息推送内容")
63+
.alias(Arrays.asList("xxxx"))
64+
.tags(Arrays.asList("group1"))
65+
.badge(999)
66+
.build()
67+
);
68+
}
69+
}

0 commit comments

Comments
 (0)