极光推送

2024-01-23

java

忍耐能化怯懦为力量,焦急却化力量为懦弱。——科尔顿

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
JPushClient jpushClient = new JPushClient(jPushMasterSecret, jPushAppKey, null, ClientConfig.getInstance());

// For push, all you need do is to build PushPayload object.
var payload = PushPayload.newBuilder()
// 推送平台设置
.setPlatform(Platform.ios())
// 推送设备指定
.setAudience(Audience.all())
// 通知内容体,是被推送到客户端的内容。与 message 一起二者必须有其一,可以二者并存。
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(dto.getMsg())
.setBadge(0)
.setSound("default")
.build())
.build())
// 应用内消息,又称作:自定义消息,透传消息。
// 此部分内容不会展示到通知栏上,JPush SDK 收到消息内容后透传给 App,需要 App 自行处理。
// iOS 在推送应用内消息通道(非 APNS)获取此部分内容,需 App 处于前台。
.setMessage(Message.content(dto.getContent()))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();

try {
PushResult result = jpushClient.sendPush(payload);
sink.success(result);
LOG.info("Got result - " + result);

} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);
sink.error(e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
sink.error(e);
}

代码仓库:

https://github.com/jpush/jpush-api-java-client

文档如下:

创建推送 API - 极光文档