package com.tcm.training.admin.utils;
|
|
import cn.jiguang.common.ClientConfig;
|
import cn.jiguang.common.resp.APIConnectionException;
|
import cn.jiguang.common.resp.APIRequestException;
|
import cn.jpush.api.JPushClient;
|
import cn.jpush.api.push.PushResult;
|
import cn.jpush.api.push.model.Platform;
|
import cn.jpush.api.push.model.PushPayload;
|
import cn.jpush.api.push.model.audience.Audience;
|
import cn.jpush.api.push.model.notification.Notification;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Map;
|
|
/**
|
* 极光推送工具类
|
*/
|
@Component
|
public class JGPushUtil {
|
|
private static final Logger log = LoggerFactory.getLogger(JGPushUtil.class);
|
|
@Value("${jpush.appKey}")
|
private String appKey;
|
|
@Value("${jpush.masterSecret}")
|
private String masterSecret;
|
|
/**
|
* 构建简单文本推送载荷。
|
*/
|
private PushPayload buildPushObject_all_all_alert(String content, String...tags) {
|
return PushPayload.newBuilder()
|
.setPlatform(Platform.all())//设置推送平台为全部平台
|
.setAudience(Audience.alias(tags))//通过别名的方式发送(用户owner_id,员工user_id)
|
.setNotification(Notification.alert(content))
|
.build();
|
}
|
|
/**
|
* 构建带标题和扩展参数的推送载荷。
|
*/
|
//TODO ios 鸿蒙配置好了之后,需要将下面的注释解除
|
private PushPayload buildPushObject_all_all_alert(String title, String content, Map<String, String> map, String...tags) {
|
return PushPayload.newBuilder()
|
.setPlatform(Platform.all())//设置推送平台为全部平台
|
.setAudience(Audience.alias(tags))//通过别名的方式发送(user_id)
|
.setNotification(Notification.android(content, title, map))
|
// .setNotification(Notification.ios(content, map))
|
// .setNotification(Notification.winphone(content, map))
|
.build();
|
}
|
|
|
/**
|
* 推送
|
* @param tags
|
* @param content
|
*/
|
public void push(String content, String...tags){
|
try {
|
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
|
PushPayload payload = this.buildPushObject_all_all_alert(content, tags);
|
PushResult result = jpushClient.sendPush(payload);
|
log.info("jpush simple push finished, statusCode={}, aliases={}", result.statusCode, tags);
|
} catch (APIConnectionException e) {
|
throw new IllegalStateException("极光推送连接失败", e);
|
} catch (APIRequestException e) {
|
throw new IllegalStateException("极光推送请求失败", e);
|
}
|
}
|
|
/**
|
* 推送带业务参数的通知消息。
|
*/
|
public void push(String content, String title, Map<String, String> map, String...tags){
|
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
|
PushPayload payload = this.buildPushObject_all_all_alert(title, content, map, tags);
|
try {
|
PushResult result = jpushClient.sendPush(payload);
|
log.info("jpush biz push finished, statusCode={}, title={}, aliases={}", result.statusCode, title, tags);
|
} catch (APIConnectionException e) {
|
throw new IllegalStateException("极光推送连接失败", e);
|
} catch (APIRequestException e) {
|
throw new IllegalStateException("极光推送请求失败", e);
|
}
|
}
|
|
}
|