package com.platform.yijia.utils; import cn.jiguang.common.ClientConfig; 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 com.alibaba.fastjson.JSON; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.time.format.DateTimeFormatter; /*** * JPushUtils * 极光推送工具类 * @author JK * @date 2020年12月29日 */ //@Component public class JPushUtils { private static Logger logger =(Logger) LoggerFactory.getLogger(JPushUtils.class); //日志 //@Value("${jpush.app.key}") private String jpushAppKey; //appKey d065738741a4ba836a47769b //@Value("${jpush.master.secret}") private String jPushMasterSecret; //masterSecret dec5b9ddf8e0834d3bfbf469 //@Value("${jpush.apns.prod}") private boolean apnsProd; //apnsProd 是否生产环境 private JPushClient jPushClient = new JPushClient(jPushMasterSecret, jpushAppKey, null, ClientConfig.getInstance()); //创建JPushClient对象 //private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //格式化时间 /* * 推送给设备标识参数的用户 * @param registrationId 设备标识 JPushInterface.init(this); registrationID = JPushInterface.getRegistrationID(this); * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏) * @param msgTitle 消息内容标题 * @param msgContent 消息内容 消息对象的JSON形式 * @param extrasParam 扩展字段 * @return false推送失败,true推送成功 */ public boolean sendToRegistrationId(String registrationId, String notificationTitle, String msgTitle, String msgContent, String extrasParam){ boolean flag = false; try { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.android_ios()) //指定要推送的平台 Platform.android_ios()安卓和iOS用户推送 .setAudience(Audience.registrationId(registrationId)) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() //android推送通知 .setTitle(notificationTitle).setAlert(notificationTitle) .addExtra("androidNotification extras key", extrasParam) .build() ).addPlatformNotification(IosNotification.newBuilder() //IOS推送通知 .setAlert(notificationTitle) .incrBadge(1) .setSound("sound.caf") .addExtra("iosNotification extras key", extrasParam) .build() ).build() ).setMessage(Message.newBuilder() .setTitle(msgTitle) .setMsgContent(msgContent) .addExtra("message extras key", extrasParam) .build() ); if(apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 //开始推送 PushResult pushResult = jPushClient.sendPush(payload.build()); if(pushResult.getResponseCode() == 200){ flag = true; //表示成功 logger.info(JSON.toJSONString(pushResult)); } } catch (Exception e) { logger.error(e.getMessage(), e); } return flag; } /* * 推送给所有用户 * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏) * @param msgTitle 消息内容标题 * @param msgContent 消息内容 消息对象的JSON形式 * @param extrasParam 扩展字段 * @return false推送失败,true推送成功 */ public boolean sendToAll(String notificationTitle, String msgTitle, String msgContent, String extrasParam){ boolean flag = false; try { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.android_ios()) //指定要推送的平台 Platform.android_ios()安卓和iOS用户推送 .setAudience(Audience.all()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() //android推送通知 .setTitle(notificationTitle).setAlert(notificationTitle) .addExtra("androidNotification extras key", extrasParam) .build() ).addPlatformNotification(IosNotification.newBuilder() //IOS推送通知 .setAlert(notificationTitle) .incrBadge(1) .setSound("sound.caf") .addExtra("iosNotification extras key", extrasParam) .build() ).build() ).setMessage(Message.newBuilder() .setTitle(msgTitle) .setMsgContent(msgContent) .addExtra("message extras key", extrasParam) .build() ); if(apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 //开始推送 PushResult pushResult = jPushClient.sendPush(payload.build()); if(pushResult.getResponseCode() == 200){ flag = true; //表示成功 logger.info(JSON.toJSONString(pushResult)); } } catch (Exception e) { logger.error(e.getMessage(), e); } return flag; } /* * IOS平台推送 * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏) * @param msgTitle 消息内容标题 * @param msgContent 消息内容 消息对象的JSON形式 * @param extrasParam 扩展字段 * @return false推送失败,true推送成功 */ public boolean sendToIOS(String notificationTitle, String msgTitle, String msgContent, String extrasParam){ boolean flag = false; try { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.ios()) // 指定要推送的平台 Platform.ios()为IOS用户推送 .setAudience(Audience.all()) // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发 .setAlert(notificationTitle) // 传一个IosAlert对象,指定apns title、title、subtitle等 .incrBadge(1) // 此项是指定此推送的badge自动加1 .setSound("sound.caf") //传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音 .addExtra("iosNotification extras key", extrasParam) // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) .build() ).build() ).setMessage(Message.newBuilder() .setTitle(msgTitle) .setMsgContent(msgContent) .addExtra("message extras key", extrasParam) .build() ); if(apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 PushResult pushResult = jPushClient.sendPush(payload.build()); //开始推送 if(pushResult.getResponseCode() == 200){ flag = true; //表示成功 logger.info(JSON.toJSONString(pushResult)); } } catch (Exception e) { logger.error(e.getMessage(), e); } return flag; } /* * Android平台推送 * @param notificationTitle 通知内容标题 发送的内容(会显示APP端的通知栏) * @param msgTitle 消息内容标题 * @param msgContent 消息内容 消息对象的JSON形式 * @param extrasParam 扩展字段 * @return false推送失败,true推送成功 */ public boolean sendToAndroid(String notificationTitle, String msgTitle, String msgContent, String extrasParam){ boolean flag = false; try { PushPayload.Builder payload = PushPayload.newBuilder() .setPlatform(Platform.android()) //指定要推送的平台 Platform.android()安卓用户推送 .setAudience(Audience.all()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setNotification(Notification.newBuilder() //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发 .addPlatformNotification(AndroidNotification.newBuilder() //指定当前推送的android通知 .setTitle(notificationTitle).setAlert(notificationTitle) .addExtra("androidNotification extras key", extrasParam) //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) .build() ).build() ).setMessage(Message.newBuilder() .setTitle(msgTitle) .setMsgContent(msgContent) .addExtra("message extras key", extrasParam) .build() ); if(apnsProd) payload.setOptions(Options.newBuilder().setApnsProduction(true).build()); //setApnsProduction的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 //开始推送 PushResult pushResult = jPushClient.sendPush(payload.build()); if(pushResult.getResponseCode() == 200){ flag = true; //表示成功 logger.info(JSON.toJSONString(pushResult)); } } catch (Exception e) { logger.error(e.getMessage(), e); } return flag; } }