1、配置类
@Data
@Component
@ConfigurationProperties(prefix = "jpush")
public class JpushProperties {
/**
* 应用的key(非登录账号)
*/
private String appKey = "099f9ed5324329b15fc0437";
/**
* 应用的密码(非登录密码)
*/
private String masterSecret = "cd15b44475s4f00609010ade";
}
2、服务类
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.file.FileClient;
import cn.jpush.api.file.model.FileType;
import cn.jpush.api.file.model.FileUploadResult;
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 com.zdxf.common.utils.StringUtils;
import com.zdxf.common.utils.push.properties.JpushProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author: zj
* @Date: 2023/10/30 0030 14:04
* @Description: 极光推送
*/
@Slf4j
@Component
public class PushClientService {
private final JPushClient jPushClient;
private final FileClient fileClient;
public PushClientService(JpushProperties properties) {
ClientConfig clientConfig = ClientConfig.getInstance();
jPushClient = new JPushClient(properties.getMasterSecret(), properties.getAppKey(), null, clientConfig);
fileClient = new FileClient(properties.getMasterSecret(), properties.getAppKey());
}
/**
* 发送给注册用户
*
* @param notificationTitle 通知内容标题
* @param msgTitle 消息内容标题
* @param msgContent 消息内容
* @param registrationIds 设备注册id
*/
public void push(String notificationTitle, String msgTitle, String msgContent,List<String> registrationIds) {
Map<String, String> extrasParam = new HashMap<>(2);
extrasParam.put("newXin", notificationTitle);
push(notificationTitle, msgTitle,msgContent,extrasParam, null, registrationIds);
}
/**
* 发送给注册用户
*
* @param notificationTitle 通知内容标题
* @param msgTitle 消息内容标题
* @param msgContent 消息内容
* @param extrasParam 扩展字段
* @param cid 消息id
* @param registrationIds 设备注册id
*/
public void push(String notificationTitle, String msgTitle, String msgContent, Map<String, String> extrasParam,
String cid, List<String> registrationIds) {
if (CollectionUtils.isEmpty(registrationIds)) {return;}
registrationIds = registrationIds.stream().filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(registrationIds)) {return;}
try {
Audience audience = Audience.registrationId(registrationIds);
PushPayload pushPayload = buildPushPayload(notificationTitle, msgTitle, msgContent, extrasParam, cid,audience);
log.info("推送参数:{}", pushPayload.toString());
PushResult pushResult = jPushClient.sendPush(pushPayload);
log.info("推送结果:{}", pushResult);
} catch (Exception e) {
e.printStackTrace();
log.error("极光推送异常:{}", e.getMessage());
}
}
/**
* 当数据太大,用文件的方式发送
* @param notificationTitle 通知内容标题
* @param msgTitle 消息内容标题
* @param msgContent 消息内容
* @param extrasParam 扩展字段
* @param cid 消息id
* @param registrationIds 设备初始化id
*/
public void pushFile(String notificationTitle, String msgTitle, String msgContent, Map<String, String> extrasParam,
String cid, List<String> registrationIds) {
try {
// 上传文件:写入本地、上传极光、最后推送
String path = "/home/watch_platform/tmp_file/" + UUID.randomUUID()+".txt";
FileUtil.writeLines(registrationIds, path, Charset.defaultCharset());
FileUploadResult fileUploadResult = fileClient.uploadFile(FileType.REGISTRATION_ID, path);
Audience audience = Audience.file(fileUploadResult.getFile_id());
PushPayload pushPayload = buildPushPayload(notificationTitle, msgTitle, msgContent, extrasParam, cid, audience);
log.info("推送参数:{}", pushPayload.toString());
PushResult pushResult = jPushClient.sendFilePush(pushPayload);
log.info("推送结果:{}", pushResult);
} catch (Exception e) {
e.printStackTrace();
log.error("极光推送异常:{}", e.getMessage());
}
}
/**
* 推送参数
*
* @param notificationTitle 通知内容标题
* @param msgTitle 消息内容标题
* @param msgContent 消息内容
* @param extrasParam 扩展字段
* @param cid 消息id
* @param audience 推送设备
* @return 推送参数
*/
private PushPayload buildPushPayload(String notificationTitle, String msgTitle, String msgContent,
Map<String, String> extrasParam, String cid, Audience audience) {
return PushPayload.newBuilder()
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
.setPlatform(Platform.android_ios())
//设置发送消息id
// .setCid(cid)
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
.setAudience(audience)
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
.setNotification(Notification.newBuilder()
.setAlert(msgTitle)
//指定当前推送的android通知
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(notificationTitle)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtras(extrasParam)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtras(extrasParam).build())
.build()
)
// Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msgContent)
.setTitle(msgTitle)
.addExtras(extrasParam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
// .setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(43200)
.build())
.build();
}
/**
* 发送测试
*
* @param args
*/
public static void main(String[] args) {
String title = "声音侦测报警";
String msgTitle = "孔明设备在15:17发现了声音侦测报警!";
Map<String, String> extrasParam = new HashMap<>(2);
extrasParam.put("notificationTitle", "我是扩展的json数据");
String cid = String.valueOf(System.currentTimeMillis());
List<String> registrationIds = CollUtil.newArrayList("1114a89fsd32b1854");
PushClientService pushClientService = new PushClientService(new JpushProperties());
pushClientService.push(title, msgTitle, msgTitle, extrasParam, cid, registrationIds);
}
}