个推工具类
项目中用到了个推,手写了个工具类。
个推文档参考:鉴权API-个推文档中心
import com.alibaba.fastjson.JSONObject;
import StringUtils;
import uuid.IdUtils;
import JedisUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.security.MessageDigest;
/**
* 个推 工具类
*
* @author zcy
* @date 2021-11-3 10:52:30
*/
public class GtUtil {
// {"msg":"success","code":0,"data":{"expire_time":"1635994647342","token":"58bc6d51d0f21b282f2174072666de3c0c3879c62e2a5066158988c6d3e3067c"}}
private static final String URL = "https://restapi.getui.com/v2/$appId";
private static final String APPKEY = "xxx";
private static final String MASTERSECRET = "xxx";
private static final int TOKENCODE = 10001;
private static final String CODE = "code";
private static final String DATA = "data";
/**
* 验证得到token
*/
public static String getToken(){
JSONObject pushNumjson = gtPushNum(JedisUtils.get("gtToken"));
if(TOKENCODE == pushNumjson.getInteger(CODE)){
JSONObject authJson = gtAuth();
if(0 == authJson.getInteger(CODE)){
JSONObject tokenJson = authJson.getJSONObject(DATA);
JedisUtils.set("gtToken",tokenJson.getString("token"),0);
}
}
return JedisUtils.get("gtToken");
}
/**
* 鉴权得到token
*/
public static JSONObject gtAuth(){
JSONObject result = null;
try {
String authUrl = URL + "auth";
long timestamp = System.currentTimeMillis();
String sign = getSha(APPKEY + timestamp + MASTERSECRET);
JSONObject json = new JSONObject();
json.put("sign",sign);
json.put("timestamp",timestamp);
json.put("appkey",APPKEY);
System.out.println("请求个推鉴权:---" + json);
//@SuppressWarnings({"resource"})
//HttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(authUrl);
StringEntity postingString = new StringEntity(json.toJSONString(),"UTF-8");
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
post.addHeader("Accept-Charset", "UTF-8");
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity(),"UTF-8");
result = (JSONObject) JSONObject.parse(content);
/**
* {"msg":"success","code":0,"data":{"expire_time":"1635899099129","token":"f748a1a05d3f2aeb4fd0c3d1529e1f196d33bca2b2ddb1ee82180f1b21b68b2f"}}
* */
System.out.println("个推鉴权返回数据:---" + result);
if(result != null && 0 == result.getInteger(CODE) && StringUtils.isNotEmpty(result.getString(DATA))){
System.out.println("个推鉴权返回data:---" + result.getString(DATA));
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 创建消息
*/
public static JSONObject gtAdd(String token,String groupName,String title,String body,String clickType,String tzUrl){
JSONObject result = null;
try {
String addUrl = URL + "push/list/message";
JSONObject json = new JSONObject();
json.put("request_id", IdUtils.fastSimpleUUID());
json.put("group_name",groupName);
JSONObject settings = new JSONObject();
settings.put("ttl",3600000);
json.put("settings",settings);
JSONObject pushMessage = new JSONObject();
JSONObject notification = new JSONObject();
notification.put("title",title);
notification.put("body",body);
notification.put("click_type",clickType);
notification.put("url",tzUrl);
pushMessage.put("notification",notification);
json.put("push_message",pushMessage);
System.out.println("个推创建消息:---" + json);
//@SuppressWarnings({"resource"})
//HttpClient httpClient = new DefaultHttpClient();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(addUrl);
StringEntity postingString = new StringEntity(json.toJSONString(),"UTF-8");
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
post.addHeader("Accept-Charset", "UTF-8");
post.addHeader("token", token);
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity(),"UTF-8");
result = (JSONObject) JSONObject.parse(content);
System.out.println("个推创建消息返回:---" + result);
if(result != null && 0 == result.getInteger(CODE) && StringUtils.isNotEmpty(result.getString(DATA))){
System.out.println("个推创建消息返回data:---" + result.getString(DATA));
}
// {"msg":"success","code":0,"data":{"taskid":"RASL_1102_8e9970fa0fc74d7389de8049fa3f059e"}}
// {"taskid":"RASL_1102_8e9970fa0fc74d7389de8049fa3f059e"}
}catch (IOException e){
e.printStackTrace();
}
return result;
}
/**
* 查询推送量
*/
public static JSONObject gtPushNum(String token){
String pushUrl = URL + "report/push/count";
JSONObject