引入maven
<!-- jiguang-sdk -->
<dependency>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.10</version>
</dependency>
编写工具类
/**
* @Author:那日花开
* @Package:com.geovis.common.core.utiles
* @Project:digit-trade-platform
* @name:JPushUtil
* @Date:2024/12/5 10:42
* @Filename:JPushUtil
*/
public class JPushUtil {
// 设置好账号的app_key和masterSecret是必须的
private static String APP_KEY = "03b344b****c94346956";
private static String MASTER_SECRET = "05c****00e0b6e62";
public static void jpushAndroid(Map<String, String> parm, List<String> registrationIds) {
//创建JPushClient(极光推送的实例)
PushApi pushApi = new PushApi.Builder()
.setAppKey(APP_KEY)
.setMasterSecret(MASTER_SECRET)
.build();
PushSendParam pushSendParam= new PushSendParam();
pushSendParam.setPlatform(Platform.android);
if(registrationIds!=null&®istrationIds.size()>0){
Audience audience =new Audience();
audience.setRegistrationIdList(registrationIds);
pushSendParam.setAudience(audience);
}else{
pushSendParam.setAudience("all");
}
NotificationMessage notificationMessage= new NotificationMessage();
notificationMessage.setAlert(parm.get("title"));
NotificationMessage.Android a=new NotificationMessage.Android();
a.setAlert( parm.get("alert"));
a.setTitle( parm.get("title"));
notificationMessage.setAndroid(a);
pushSendParam.setNotification(notificationMessage);
Options options= new Options();
options.setApnsProduction(true);
pushSendParam.setOptions(options);
pushApi.send(pushSendParam);
}
//极光推送>>ios
//Map<String, String> parm是我自己传过来的参数,可以自定义参数
public static void jpushIOS(Map<String, Object> parm, List<String> registrationIds) {
//创建JPushClient
PushApi pushApi = new PushApi.Builder()
.setAppKey(APP_KEY)
.setMasterSecret(MASTER_SECRET)
.build();
PushSendParam pushSendParam= new PushSendParam();
pushSendParam.setPlatform(Platform.ios);
if(registrationIds!=null&®istrationIds.size()>0){
Audience audience =new Audience();
audience.setRegistrationIdList(registrationIds);
pushSendParam.setAudience(audience);
}else{
pushSendParam.setAudience("all");
}
NotificationMessage notificationMessage= new NotificationMessage();
notificationMessage.setAlert(parm.get("alert")+"");
NotificationMessage.IOS a=new NotificationMessage.IOS();
a.setAlert(parm.get("alert"));
a.setExtras(parm);
pushSendParam.setNotification(notificationMessage);
Options options= new Options();
options.setApnsProduction(true);
pushSendParam.setOptions(options);
pushApi.send(pushSendParam);
}
//极光推送>>All所有平台
public static void jpushAll(Map<String, Object> parm, List<String> registrationIds) {
//创建JPushClient
PushApi pushApi = new PushApi.Builder()
.setAppKey(APP_KEY)
.setMasterSecret(MASTER_SECRET)
.build();
PushSendParam pushSendParam= new PushSendParam();
pushSendParam.setPlatform(ApiConstants.Platform.ALL);
if(registrationIds!=null&®istrationIds.size()>0){
Audience audience =new Audience();
audience.setRegistrationIdList(registrationIds);
pushSendParam.setAudience(audience);
}else{
pushSendParam.setAudience(ApiConstants.Audience.ALL);
}
NotificationMessage notificationMessage= new NotificationMessage();
notificationMessage.setAlert(parm.get("alert")+"");
NotificationMessage.IOS a=new NotificationMessage.IOS();
a.setAlert(parm.get("alert"));
a.setExtras(parm);
notificationMessage.setIos(a);
NotificationMessage.Android b=new NotificationMessage.Android();
b.setAlert(""+ parm.get("alert"));
b.setTitle(String.valueOf(parm.get("title")));
b.setCategory("0");
b.setExtras(parm);
notificationMessage.setAndroid(b);
NotificationMessage.HMOS c=new NotificationMessage.HMOS();
c.setAlert(""+parm.get("alert"));
c.setCategory("0");
c.setExtras(parm);
c.setBadgeAddNumber(1);
notificationMessage.setHmos(c);
pushSendParam.setNotification(notificationMessage);
Options options= new Options();
options.setApnsProduction(true);
pushSendParam.setOptions(options);
pushApi.send(pushSendParam);
}
public static void main(String[] args) {
Map<String, String> parm = new HashMap<>();
parm.put("title","我是通知");
parm.put("alert","tong zhi nei rong");
List<String> registrationIds= new ArrayList<>();
registrationIds.add("1104a89793af2cfc030");
jpushAndroid(parm,registrationIds);
}
}