package com.ks.push.utils.push;
|
|
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.Message;
|
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.AndroidNotification;
|
import cn.jpush.api.push.model.notification.Notification;
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.gson.Gson;
|
import com.google.gson.JsonObject;
|
import com.ks.push.utils.JPushUtil;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.yeshi.utils.push.entity.PushAppInfo;
|
import org.yeshi.utils.push.entity.PushMessage;
|
|
import java.util.Arrays;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author hxh
|
* @title: JpushUtil
|
* @description: 极光推送
|
* @date 2021/12/2 15:34
|
*/
|
public class JpushUtil {
|
|
private static Logger LOG = LoggerFactory.getLogger(JpushUtil.class);
|
|
|
/**
|
* @return java.lang.String
|
* @author hxh
|
* @description 推送通知
|
* @date 15:59 2021/12/2
|
* @param: appInfo
|
* @param: message
|
* @param: regIds
|
* @param: aliasList
|
**/
|
public static String pushNotification(PushAppInfo appInfo, PushMessage message, List<String> regIds, List<String> aliasList) {
|
JPushClient jpushClient = new JPushClient(appInfo.getAppSecret(), appInfo.getAppKey(), null, ClientConfig.getInstance());
|
|
Map<String, String> params;
|
if (message.getActivity() != null) {
|
params = new HashMap<>();
|
params.put("activity", message.getActivity());
|
params.put("params", new Gson().toJson(message.getActivityParams()));
|
} else {
|
params = message.getActivityParams();
|
}
|
|
AndroidNotification.Builder androidBuilder = AndroidNotification.newBuilder().setAlert(message.getContent()).setTitle(message.getTitle());
|
|
//通过intent推送
|
if (message.getActivity().startsWith("intent:#")) {
|
JsonObject intent = new JsonObject();
|
intent.addProperty("url", message.getActivity());
|
androidBuilder = androidBuilder.setIntent(intent).addExtras(message.getActivityParams());
|
} else {
|
androidBuilder = androidBuilder.addExtras(params);
|
}
|
|
PushPayload.Builder payloadBuilder = PushPayload.newBuilder().
|
setPlatform(Platform.all())
|
.setNotification(Notification.newBuilder()
|
.addPlatformNotification(androidBuilder.build())
|
.build());
|
loadTarget(payloadBuilder, regIds, aliasList);
|
PushPayload payload = payloadBuilder.build();
|
|
|
try {
|
PushResult result = jpushClient.sendPush(payload);
|
LOG.info("Got result - " + result);
|
return result.msg_id + "";
|
} catch (APIConnectionException e) {
|
LOG.error("Connection error, should retry later", e);
|
} catch (APIRequestException e) {
|
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 null;
|
}
|
|
private static void loadTarget(PushPayload.Builder builder, List<String> regIds, List<String> aliasList) {
|
if (regIds != null) {
|
builder.setAudience(Audience.registrationId(regIds));
|
} else if (aliasList != null) {
|
builder.setAudience(Audience.alias(aliasList));
|
} else {
|
builder.setAudience(Audience.all());
|
}
|
}
|
|
|
/**
|
* @return java.lang.String
|
* @author hxh
|
* @description 推送透传消息
|
* @date 11:48 2022/1/12
|
* @param: appInfo
|
* @param: data
|
* @param: regIds
|
* @param: aliasList
|
**/
|
public static String pushMessage(PushAppInfo appInfo, JSONObject data, List<String> regIds, List<String> aliasList) {
|
JPushClient jpushClient = new JPushClient(appInfo.getAppSecret(), appInfo.getAppKey(), null, ClientConfig.getInstance());
|
|
PushPayload.Builder payloadBuilder = PushPayload.newBuilder().
|
setPlatform(Platform.all())
|
.setMessage(Message.newBuilder()
|
.setMsgContent(data.toJSONString())
|
.build());
|
loadTarget(payloadBuilder, regIds, aliasList);
|
|
PushPayload payload = payloadBuilder.build();
|
|
try {
|
PushResult result = jpushClient.sendPush(payload);
|
LOG.info("Got result - " + result);
|
return result.msg_id + "";
|
} catch (APIConnectionException e) {
|
LOG.error("Connection error, should retry later", e);
|
} catch (APIRequestException e) {
|
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 null;
|
}
|
|
public static void main(String[] args) {
|
Map<String, String> activityParams = new HashMap<>();
|
activityParams.put("type", "web");
|
JSONObject jumpParams = new JSONObject();
|
jumpParams.put("url", "http://www.baidu.com");
|
activityParams.put("params", jumpParams.toJSONString());
|
|
PushAppInfo appInfo = new PushAppInfo();
|
appInfo.setAppKey("6f219d3bfa78428537090fef");
|
appInfo.setAppSecret("c48f35cd1cef4b717f721e4b");
|
|
|
PushMessage pushMessage = new PushMessage();
|
pushMessage.setActivity(JPushUtil.createIntent("com.yeshi.makemoney.video.app.push.PushOpenClickActivity", "com.yeshi.makemoney.video"));
|
pushMessage.setActivityParams(activityParams);
|
pushMessage.setTitle("测试标题");
|
pushMessage.setContent("测试内容");
|
pushNotification(appInfo, pushMessage, Arrays.asList(new String[]{"1507bfd3f737761f2f2"}), null);
|
}
|
|
|
}
|