1.pom.xml
<!--极光推送-->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.6</version>
</dependency>
2.JpushUtils.java
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.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
public class JpushUtils{
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);
protected static final String APP_KEY ="xxxxxx";
protected static final String MASTER_SECRET = "xxxxxxx";
/**
* 提醒tag
**/
public static final String NOTICE_TAG = "20";
/**
* 标题
**/
public static final String NOTICE_TITLE = "xxxx提醒";
/**
* 消息体
**/
public static final String NOTICE_MSG = "xxxx{0}待确认,请及时处理!";
/**
* @Description: 发送app极光消息
* @Author: duyaqiong
* @Date: 2020/9/11 16:11
* @Return: void
**/
public static void sendJGNotice(String mobile,String orderNo)
{
//创建JPushClient
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
// 自定义参数
Map<String, String> params = new HashMap<String, String>();
params.put("orderNo",orderNo);
// 必须参数
Map<String, String> alias = new HashMap<String, String>();
alias.put("tag",JpushUtils.NOTICE_TAG);
alias.put("mobile",mobile);
alias.put("title",JpushUtils.NOTICE_TITLE);
alias.put("msg",JpushUtils.formatMsgStr(JpushUtils.NOTICE_MSG,new String[]{orderNo}));
alias.put("param", JSONObject.toJSONString(params));
PushPayload payload = buildPushObject_all_alias_alert(alias);
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Get result - " + result);
} catch (APIConnectionException e) {
e.printStackTrace();
LOG.error("Connection error. Should retry later. ", e);
LOG.error("Sendno: " + payload.getSendno());
} catch (APIRequestException e) {
e.printStackTrace();
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
LOG.error("Sendno: " + payload.getSendno());
}
}
public static PushPayload buildPushObject_all_alias_alert(Map<String, String> parm) {
String sound = "default";
if(!StringUtils.isEmpty(parm.get("tag"))){
sound = "jpush_prompt_tone" + parm.get("tag") + ".mp3";
}
return PushPayload.newBuilder()
//所有平台的用户
.setPlatform(Platform.all())
.setAudience(Audience.alias(parm.get("mobile")))
.setNotification(Notification.newBuilder()
//发送ios
.addPlatformNotification(IosNotification.newBuilder()
//消息体
.setAlert(parm.get("msg"))
.setBadge(+1)
//ios提示音
.setSound(sound)
//附加参数
.addExtras(parm)
.build())
//发送android
.addPlatformNotification(AndroidNotification.newBuilder()
//消息体
.setAlert(parm.get("msg"))
.setTitle(parm.get("title"))
.addExtras(parm)
.build())
.build())
.build();
}
/**
* @Description:格式化消息体
* @Author: duyaqiong
* @Date: 2020/9/15 9:43
* @param msg
* @param params
* @Return: java.lang.String
**/
public static String formatMsgStr(String msg,String[] params){
return MessageFormat.format(msg, params);
}
}
3.使用
JpushUtils.sendJGNotice(mobile,orderNo);