/**
* @description: 极光推送
* @author: DAIHAO
* @time: 2020/11/13 13:59
*/
public class JiGuangPush {
private static final Logger log = LoggerFactory.getLogger(JiGuangPush.class);
private static String masterSecret = "";
private static String appKey = "";
private static String pushUrl = "https://api.jpush.cn/v3/push";
private static boolean apns_production = true;
private static int time_to_live = 86400;
/**
* 极光推送
*/
public static void jiGuangPush(JSONArray alias, String alert, JSONObject extras) {
try {
String result = push(pushUrl, alias, alert, appKey, masterSecret, apns_production, time_to_live, extras);
JSONObject resData = (JSONObject) FASTJSON.parse(result);
if (resData.containsKey("error")) {
log.info("针对别名为" + alias + "的信息推送失败!");
JSONObject error = (JSONObject) resData.get("error");
log.info("错误信息为:" + error.get("message").toString());
}
log.info("针对别名为" + alias + "的信息推送成功!");
} catch (Exception e) {
log.error("针对别名为" + alias + "的信息推送失败!", e);
}
}
/**
* 组装极光推送专用json串
*
* @param alias
* @param alert
* @return json
*/
public static JSONObject generateJson(JSONArray alias, String alert, boolean apns_production, int time_to_live, JSONObject extras) {
JSONObject json = new JSONObject();
JSONArray platform = new JSONArray();//平台
platform.add("android");
platform.add("ios");
JSONObject audience = new JSONObject();//推送目标
audience.put("alias", alias);
JSONObject notification = new JSONObject();//通知内容
JSONObject android = new JSONObject();//android通知内容
android.put("alert", alert);
android.put("builder_id", 1);
android.put("extras", extras);
android.put("badge_add_num", 1);//角标
android.put("badge_class", "");
JSONObject ios = new JSONObject();//ios通知内容
ios.put("alert", alert);
ios.put("sound", "default");
ios.put("badge", "+1");
ios.put("extras", extras);
notification.put("android", android);
notification.put("ios", ios);
JSONObject options = new JSONObject();//设置参数
options.put("time_to_live", Integer.valueOf(time_to_live));
options.put("apns_production", apns_production);
json.put("platform", platform);
json.put("audience", audience);
json.put("notification", notification);
json.put("options", options);
return json;
}
/**
* 推送方法-调用极光API
*
* @param reqUrl
* @param alias
* @param alert
* @return result
*/
public static String push(String reqUrl, JSONArray alias, String alert, String appKey, String masterSecret, boolean apns_production, int time_to_live, JSONObject extras) {
Base64.Encoder encoder = Base64.getEncoder();
String base64_auth_string = appKey + ":" + masterSecret;
base64_auth_string = encoder.encodeToString(base64_auth_string.getBytes());
String authorization = "Basic " + base64_auth_string;
return sendPostRequest(reqUrl, generateJson(alias, alert, apns_production, time_to_live, extras).toString(), "UTF-8", authorization);
}
/**
* 发送Post请求(json格式)
*
* @param reqURL
* @param data
* @param encodeCharset
* @param authorization
* @return result
*/
@SuppressWarnings({"resource"})
public static String sendPostRequest(String reqURL, String data, String encodeCharset, String authorization) {
HttpPost httpPost = new HttpPost(reqURL);
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
String result = "";
try {
StringEntity entity = new StringEntity(data, encodeCharset);
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Authorization", authorization.trim());
response = client.execute(httpPost);
result = EntityUtils.toString(response.getEntity(), encodeCharset);
} catch (Exception e) {
log.error("请求通信[" + reqURL + "]时偶遇异常,堆栈轨迹如下", e);
} finally {
client.getConnectionManager().shutdown();
}
return result;
}
public static void main(String[] args) throws UnsupportedEncodingException {
JSONArray alias = FASTJSON.newArr();
alias.add("-----");
String alert = "极光推送测试";
JSONObject extras = FASTJSON.newDoc();
extras.put("id", "1");
jiGuangPush(alias, alert, extras);
}
}
注意几点:
Authen failed
可能的情况
我出现的情况是开始没有使用BASE64加密,这里使用Java自带BASE64工具类加密可用。
The alias has invalid character
这里错误是我使用的是List 应该使用JSONArray。