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.Notification;
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.gson.Gson;
|
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();
|
}
|
|
PushPayload.Builder payloadBuilder = PushPayload.newBuilder().
|
setPlatform(Platform.all())
|
.setNotification(Notification.android(message.getContent(), message.getTitle(), params));
|
|
|
if (regIds != null) {
|
payloadBuilder.setAudience(Audience.registrationId(regIds));
|
} else if (aliasList != null) {
|
payloadBuilder.setAudience(Audience.alias(aliasList));
|
} else {
|
payloadBuilder.setAudience(Audience.all());
|
}
|
|
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;
|
}
|
|
|
/**
|
* @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());
|
if (regIds != null) {
|
payloadBuilder.setAudience(Audience.registrationId(regIds));
|
} else if (aliasList != null) {
|
payloadBuilder.setAudience(Audience.alias(aliasList));
|
} else {
|
payloadBuilder.setAudience(Audience.all());
|
}
|
|
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("activity", "test");
|
activityParams.put("type", "requestLocation");
|
JSONObject jumpParams = new JSONObject();
|
jumpParams.put("id", 123123);
|
activityParams.put("params", jumpParams.toJSONString());
|
|
PushAppInfo appInfo = new PushAppInfo();
|
appInfo.setAppKey("987d3d50f209994fa522d001");
|
appInfo.setAppSecret("e4053447d0014576ea2c47ab");
|
// PushMessage message = new PushMessage("你好啊", "下午好,hello world", null, null, null, activityParams);
|
|
Map<String,Object> dataMap=new HashMap<>();
|
dataMap.put("type","requestLocation");
|
|
JSONObject data = new JSONObject(dataMap);
|
pushMessage(appInfo, data, Arrays.asList(new String[]{"100d8559098624d84e6"}), null);
|
}
|
|
|
}
|