- 首先引入maven依賴 或 直接下載jar包引用,具體可看官方文檔:https://github.com/jpush/jpush-api-java-client
- 我是通過maven依賴,maven配置文件添加 以下包
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.10</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.1.4</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.6.Final</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <!-- For log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
- 在網上找到 並修改後的JpushUtil工具類;親測可用
package org.jeecg.common.util;
import java.util.ArrayList;
import java.util.List;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.Options;
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 cn.jpush.api.schedule.ScheduleResult;
import lombok.extern.slf4j.Slf4j;/**
*
* @ClassName: JpushUtil
* @Description: 極光推送工具類
* @author WangJing
* @date 2020年6月12日
*
*/
@Slf4j
public class JpushUtil {
private final static String APPKEY = "********";private final static String MASTERSECRET = "********";
private static JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKEY);
/**
* 發送給標識用戶
* @param alias 設備標識
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @param uuid 日誌記錄標識
* @return false推送失敗,true推送成功
*/
public static Boolean sendToAlias(List<String> alias, String notificationTitle, String msgTitle, String msgContent,
String extrasparam, String uuid) {
Boolean result = false;
try {
PushPayload pushPayload = buildPushObject(alias, notificationTitle, msgTitle, msgContent, extrasparam);
log.info("uuid:{}, JpushUtil--sendToAlias--jpush--participation:{} ", uuid,
pushPayload.toJSON().toString());
PushResult sendPush = jPushClient.sendPush(pushPayload);
if (sendPush.getResponseCode() == 200) {
result = true;
}
log.info("uuid:{}, JpushUtil--sendToAlias--jpush--outParameter:{} ", uuid, sendPush.toString());
} catch (APIConnectionException | APIRequestException e) {
log.error("uuid:{}, JpushUtil--sendToAlias--error:{} ", uuid, e);
}
return result;
}
/**
* 定時發送給標識用戶
* @param alias 設備標識
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @param scheduleName 日程名稱
* @param scheduleTime 日程時間(格式:yyyy-MM-dd HH:mm:ss)
* @param uuid 日誌記錄標識
* @return false推送失敗,true推送成功
*/
public static Boolean sendToAliasTiming(List<String> alias, String notificationTitle, String msgTitle,
String msgContent, String extrasparam, String scheduleName, String scheduleTime, String uuid) {
Boolean result = false;
try {
PushPayload pushPayload = buildPushObject(alias, notificationTitle, msgTitle, msgContent, extrasparam);
log.info("uuid:{}, JpushUtil--sendToAliasTiming--jpush--participation:{} ", uuid,
pushPayload.toJSON().toString());
ScheduleResult createSingleSchedule = jPushClient.createSingleSchedule(scheduleName, scheduleTime,
pushPayload);
if (createSingleSchedule.getResponseCode() == 200) {
result = true;
}
log.info("uuid:{}, JpushUtil--sendToAliasTiming--jpush--outParameter:{} ", uuid,
createSingleSchedule.toString());
} catch (APIConnectionException | APIRequestException e) {
log.error("uuid:{}, JpushUtil--sendToAliasTiming--error:{} ", uuid, e);
}
return result;
}
/**
* 發送給所有安卓用戶
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @param uuid 日誌記錄標識
* @return false推送失敗,true推送成功
*/
public static Boolean sendToAndroid(String notificationTitle, String msgTitle, String msgContent,
String extrasparam, String uuid) {
Boolean result = false;
try {
PushPayload pushPayload = buildPushObjectByAndroid(notificationTitle, msgTitle, msgContent, extrasparam);
log.info("uuid:{}, JpushUtil--sendToAndroid--jpush--participation:{} ", uuid,
pushPayload.toJSON().toString());
PushResult sendPush = jPushClient.sendPush(pushPayload);
if (sendPush.getResponseCode() == 200) {
result = true;
}
log.info("uuid:{}, JpushUtil--sendToAndroid--jpush--outParameter:{} ", uuid, sendPush.toString());
} catch (APIConnectionException | APIRequestException e) {
log.error("uuid:{}, JpushUtil--sendToAndroid--error:{} ", uuid, e);
}
return result;
}
/**
* 發送給所有IOS用戶
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @param uuid 日誌記錄標識
* @return false推送失敗,true推送成功
*/
public static Boolean sendToIos(String notificationTitle, String msgTitle, String msgContent, String extrasparam,
String uuid) {
Boolean result = false;
try {
PushPayload pushPayload = buildPushObjectByIos(notificationTitle, msgTitle, msgContent, extrasparam);
log.info("uuid:{}, JpushUtil--sendToIos--jpush--participation:{} ", uuid, pushPayload.toJSON().toString());
PushResult sendPush = jPushClient.sendPush(pushPayload);
if (sendPush.getResponseCode() == 200) {
result = true;
}
log.info("uuid:{}, JpushUtil--sendToIos--jpush--outParameter:{} ", uuid, sendPush.toString());
} catch (APIConnectionException | APIRequestException e) {
log.error("uuid:{}, JpushUtil--sendToIos--error:{} ", uuid, e);
}
return result;
}
/**
* 發送給所有用戶
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @param uuid 日誌記錄標識
* @return false推送失敗,true推送成功
*/
public static int sendToAll(String notificationTitle, String msgTitle, String msgContent, String extrasparam,
String uuid) {
int result = 0;
try {
PushPayload pushPayload = buildPushObjectByAll(notificationTitle, msgTitle, msgContent, extrasparam);
log.info("uuid:{}, JpushUtil--sendToAll--jpush--participation:{} ", uuid, pushPayload.toJSON().toString());
PushResult sendPush = jPushClient.sendPush(pushPayload);
if (sendPush.getResponseCode() == 200) {
result = 1;
}
log.info("uuid:{}, JpushUtil--sendToAll--jpush--outParameter:{} ", uuid, sendPush.toString());
} catch (APIConnectionException | APIRequestException e) {
log.error("uuid:{}, JpushUtil--sendToAll--error:{} ", uuid, e);
}
return result;
}
/**
* 定時發送給所有用戶
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @param scheduleName 日程名稱
* @param scheduleTime 日程時間(格式:yyyy-MM-dd HH:mm:ss)
* @param uuid 日誌記錄標識
* @return false推送失敗,true推送成功
*/
public static Boolean sendToAllTiming(String notificationTitle, String msgTitle, String msgContent,
String extrasparam, String scheduleName, String scheduleTime, String uuid) {
Boolean result = false;
try {
PushPayload pushPayload = buildPushObjectByAll(notificationTitle, msgTitle, msgContent, extrasparam);
log.info("uuid:{}, JpushUtil--sendToAllTiming--jpush--participation:{} ", uuid,
pushPayload.toJSON().toString());
ScheduleResult createSingleSchedule = jPushClient.createSingleSchedule(scheduleName, scheduleTime,
pushPayload);
if (createSingleSchedule.getResponseCode() == 200) {
result = true;
}
log.info("uuid:{}, JpushUtil--sendToAllTiming--jpush--outParameter:{} ", uuid,
createSingleSchedule.toString());
} catch (APIConnectionException | APIRequestException e) {
log.error("uuid:{}, JpushUtil--sendToAllTiming--error:{} ", uuid, e);
}
return result;
}
/**
* 組合發送對象(所有用戶)
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @return
*/
public static PushPayload buildPushObjectByAll(String notificationTitle, String msgTitle, String msgContent,
String extrasparam) {
return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
.setNotification(Notification.newBuilder().setAlert(msgContent)
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(msgContent)
.setTitle(notificationTitle).addExtra("url", extrasparam).build())
.addPlatformNotification(IosNotification.newBuilder().setAlert(msgContent).incrBadge(1)
.setSound("sound.caf").addExtra("url", extrasparam).build())
.build())
.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
.addExtra("url", extrasparam).build())
.setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
.build();
}
/**
* 組合發送對象(標識用戶)
* @param alias 用戶標識
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @return
*/
private static PushPayload buildPushObject(List<String> alias, String notificationTitle, String msgTitle,
String msgContent, String extrasparam) {
return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(msgContent)
.setTitle(notificationTitle).addExtra("url", extrasparam).build())
.addPlatformNotification(IosNotification.newBuilder().setAlert(msgContent).incrBadge(1)
.setSound("sound.caf").addExtra("url", extrasparam).build())
.build())
.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
.addExtra("url", extrasparam).build())
.setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
.build();}
/**
* 組合發送對象(Android用戶)
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @return
*/
private static PushPayload buildPushObjectByAndroid(String notificationTitle, String msgTitle, String msgContent,
String extrasparam) {
return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(msgContent)
.setTitle(notificationTitle).addExtra("url", extrasparam).build())
.build())
.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
.addExtra("url", extrasparam).build())
.setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
.build();
}
/**
* 組合發送對象(ios用戶)
* @param notificationTitle 通知內容標題
* @param msgTitle 消息內容標題
* @param msgContent 消息內容
* @param extrasparam 擴展字段(傳跳轉的鏈接)
* @return
*/
private static PushPayload buildPushObjectByIos(String notificationTitle, String msgTitle, String msgContent,
String extrasparam) {
return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder().setAlert(msgContent).incrBadge(1)
.setSound("sound.caf").addExtra("url", extrasparam).build())
.build())
.setMessage(Message.newBuilder().setMsgContent(msgContent).setTitle(msgTitle)
.addExtra("url", extrasparam).build())
.setOptions(Options.newBuilder().setApnsProduction(true).setSendno(1).setTimeToLive(86400).build())
.build();
}
}
注:以上內容僅提供參考和交流,請勿用於商業用途,如有侵權聯繫本人刪除!