SpringBoot集成极光推送
1.添加pom依赖,注意依赖版本问题
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
2.在控制器方法中设置好在jPush官网申请的appKey和masterSecret密码
@Resource
PushService pushService;
@GetMapping("send/{msg}")
@ResponseBody
public String sendMessage(@PathVariable String msg){
JPushClient jPushClient = new JPushClient(MASTER_SECRET,APP_KEY,null, ClientConfig.getInstance());
PushPayload payload = pushService.sendPush(msg);
return "发送完成!";
}
@Service
public class PushService {
protected static final Logger LOG = LoggerFactory.getLogger(PushService.class);
protected static final String APP_KEY = "****************";
protected static final String MASTER_SECRET = "**************************";
protected static final String GROUP_PUSH_KEY = "************************";
protected static final String GROUP_MASTER_SECRET = "*********************";
public static final String TITLE = "Test from API example";
public static final String ALERT = "Test from API Example - alert";
public static final String MSG_CONTENT = "Test from API Example - msgContent";
public static final String REGISTRATION_ID = "0900e8d85ef";
public static final String TAG = "tag_api";
public static long sendCount = 0;
private static long sendTotalTime = 0;
public static PushPayload sendPush(String msg){
ClientConfig clientConfig = ClientConfig.getInstance();
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
final PushPayload payload = buildPushObject_all_all_alertWithTitle(msg);
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result -"+result);
} catch (APIConnectionException e) {
LOG.error("Connection error,should retry later",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());
}
return payload;
}
public static PushPayload buildPushObject_all_all_alert() {
return PushPayload.alertAll(ALERT);
}
public static PushPayload buildPushObject_all_all_alertWithTitle(String msg){
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.all())
.setNotification(Notification.alert(msg))
.setMessage(Message.content(msg))
.build();
}
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.tag("tag1"))
.setNotification(Notification.android(ALERT, TITLE, null))
.build();
}
}