SpringBoot整合极光推送
程序员文章站
2022-07-09 16:35:40
SpringBoot整合极光推送maven依赖配置类推送类maven依赖 cn.jpush.api jpush-client 3.3.10
maven依赖
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.10</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jsms-client</artifactId> <version>1.2.9</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency>
配置类
################### 极光推送配置 ###################
jpush: # AppKey
appKey: ********** # 密钥
masterSecret: ************* # 短信模板ID
smsTempId: *** # 离线消息保留时长(秒) liveTime: 86400
package com.manage.framework.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "jpush") public class JPushConfig { /**
* AppKey
*/ private String appKey; /**
* 密钥
*/ private String masterSecret; private String groupKey; private String groupMasterSecert; /**
* 短信模板ID
*/ private int smsTempId; /**
* 离线消息保留时长(秒)
*/ private int liveTime; public String getAppKey() { return appKey; } public void setAppKey(String appKey) { this.appKey = appKey; } public String getMasterSecret() { return masterSecret; } public void setMasterSecret(String masterSecret) { this.masterSecret = masterSecret; } public String getGroupKey() { return groupKey; } public void setGroupKey(String groupKey) { this.groupKey = groupKey; } public String getGroupMasterSecert() { return groupMasterSecert; } public void setGroupMasterSecert(String groupMasterSecert) { this.groupMasterSecert = groupMasterSecert; } public int getSmsTempId() { return smsTempId; } public void setSmsTempId(int smsTempId) { this.smsTempId = smsTempId; } public int getLiveTime() { return liveTime; } public void setLiveTime(int liveTime) { this.liveTime = liveTime; } }
JPush常量
package com.manage.framework.jpush; /**
* JPush常量
*/ public class JPushConstants { /** 指定推送平台 */ public static final String PLATFORM_ALL = "all"; public static final String PLATFORM_ANDROID = "android"; public static final String PLATFORM_IOS = "ios"; public static final String PLATFORM_WINPHONE = "winphone"; public static final String PLATFORM_ANDROID_IOS = "android_ios"; public static final String PLATFORM_ANDROID_WINPHONE = "android_winphone"; public static final String PLATFORM_IOS_WINPHONE = "ios_winphone"; /** 指定推送目标 */ /** 广播推送(全部设备) */ public static final String AUDIENCE_ALL = "all"; /** 标签推送,多个标签之间是 OR 的关系,即取并集。一次推送最多 20 个。 */ public static final String AUDIENCE_TAG_AND = "tag_and"; /** 标签推送,多个标签之间是 AND 关系,即取交集。一次推送最多 20 个。 */ public static final String AUDIENCE_TAG_NOT = "tag_not"; /** 标签推送,多个标签之间,先取多标签的并集,再对该结果取补集。一次推送最多 20 个。 */ public static final String AUDIENCE_TAG = "tag"; /** 别名推送,多个别名之间是 OR 关系,即取并集。一次推送最多 1000 个。 */ public static final String AUDIENCE_ALIAS = "alias"; /** 注册ID推送,多个注册ID之间是 OR 关系,即取并集。一次推送最多 1000 个。 */ public static final String AUDIENCE_REGISTRATION_ID = "registration_id"; /** 用户分群ID推送,定义为数组,但目前限制一次只能推送一个。 */ public static final String AUDIENCE_SEGMENT = "segment"; /** A/B Test ID推送,定义为数组,但目前限制是一次只能推送一个。 */ public static final String AUDIENCE_ABTEST = "abTest"; /** 指定通知对象 */ public static final String NOTIFICATION_ANDROID = "android"; public static final String NOTIFICATION_IOS = "ios"; public static final String NOTIFICATION_ANDROID_IOS = "android_ios"; public static final String NOTIFICATION_WINPHONE = "winphone"; }
推送类
package com.manage.framework.jpush; import cn.jiguang.common.ClientConfig; import cn.jiguang.common.ServiceHelper; import cn.jiguang.common.connection.NativeHttpClient; 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.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.jsms.api.SendSMSResult; import cn.jsms.api.common.SMSClient; import cn.jsms.api.common.model.BatchSMSPayload; import cn.jsms.api.common.model.BatchSMSResult; import cn.jsms.api.common.model.RecipientPayload; import cn.jsms.api.common.model.SMSPayload; import com.alibaba.fastjson.JSONObject; import com.manage.framework.config.JPushConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; /**
* 极光推送push消息Service
*/ @Service public class JPushClientService { private final static Logger log = LoggerFactory.getLogger(JPushClientService.class); @Autowired private JPushConfig jPushConfig; /**
* 推送push消息
*
* @param platform 指定推送平台
* @param audience 指定推送范围
* @param audienceValues 指定推送目标
* @param title 通知标题
* @param message 通知内容
* @param extras 扩展字段
* @return int
*/ public int sendPush(String platform, String audience, List<String> audienceValues, String title, String message, String extras) { // 构建推送对象 PushPayload payload = buildPushPayload(platform, audience, audienceValues, title, message, extras); // 推送push消息 return sendPush(payload); } /**
* 推送push消息
*
* @param payload 推送对象
* @return int
*/ public int sendPush(PushPayload payload) { log.info("开始推送push消息"); ClientConfig clientConfig = ClientConfig.getInstance(); ClientConfig.getInstance().setPushHostName("https://bjapi.push.jiguang.cn"); String authCode = ServiceHelper.getBasicAuthorization(jPushConfig.getAppKey(), jPushConfig.getMasterSecret()); NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig); JPushClient jpushClient = new JPushClient(jPushConfig.getMasterSecret(), jPushConfig.getAppKey(), null, clientConfig); jpushClient.getPushClient().setHttpClient(httpClient); try { PushResult res = jpushClient.sendPush(payload); if (res.isResultOK()) { log.info("推送成功,PushResult:{}", res); return 2; } else { log.info("推送失败,PushResult:{}", res); return 3; } } catch (APIConnectionException e) { log.error("Connection error. Should retry later. ", e); log.error("Sendno: " + payload.getSendno()); return 3; } catch (APIRequestException e) { 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()); return 3; } } /**
* 构建推送对象
*
* @param platform 指定推送平台
* @param audience 指定推送范围
* @param audienceValues 指定推送目标
* @param title 通知标题
* @param message 通知内容
* @param extras 扩展字段
* @return PushPayload
*/ public PushPayload buildPushPayload(String platform, String audience, List<String> audienceValues, String title, String message, String extras) { return PushPayload.newBuilder() .setPlatform(buildPlatform(platform)) // Platform指定推送平台 .setAudience(buildAudience(audience, audienceValues)) // Audience指定推送目标 .setNotification(buildNotification(title, message, extras)) // Notification推送通知内容体 .setOptions(Options.newBuilder().setTimeToLive(jPushConfig.getLiveTime()).build()) // Options推送参数,设置离线消息保留时长 .build(); } /**
* 构建推送平台
*
* @param platform 指定推送平台
* @return Platform
*/ private static Platform buildPlatform(String platform) { switch (platform) { case JPushConstants.PLATFORM_ALL: return Platform.all(); case JPushConstants.PLATFORM_ANDROID: return Platform.android(); case JPushConstants.PLATFORM_IOS: return Platform.ios(); case JPushConstants.PLATFORM_WINPHONE: return Platform.winphone(); case JPushConstants.PLATFORM_ANDROID_IOS: return Platform.android_ios(); case JPushConstants.PLATFORM_ANDROID_WINPHONE: return Platform.android_winphone(); case JPushConstants.PLATFORM_IOS_WINPHONE: return Platform.ios_winphone(); } return Platform.all(); } /**
* 构建推送目标
*
* @param audience 指定推送范围
* @param audienceValues 指定推送目标
* @return Audience
*/ private static Audience buildAudience(String audience, List<String> audienceValues) { switch (audience) { case JPushConstants.AUDIENCE_ALL: return Audience.all(); case JPushConstants.AUDIENCE_TAG: return Audience.tag(audienceValues); case JPushConstants.AUDIENCE_TAG_AND: return Audience.tag_and(audienceValues); case JPushConstants.AUDIENCE_TAG_NOT: return Audience.tag_not(audienceValues); case JPushConstants.AUDIENCE_ALIAS: return Audience.alias(audienceValues); case JPushConstants.AUDIENCE_REGISTRATION_ID: return Audience.registrationId(audienceValues); case JPushConstants.AUDIENCE_SEGMENT: return Audience.segment(audienceValues); case JPushConstants.AUDIENCE_ABTEST: return Audience.abTest(audienceValues); } return Audience.all(); } /**
* 构建通知内容体
*
* @param title 通知标题
* @param message 通知内容
* @param extras 扩展字段
* @return Notification
*/ private static Notification buildNotification(String title, String message, String extras) { Notification.Builder notification = Notification.newBuilder() .setAlert(message) // alert通知,推送到Platform指定的多个平台 .addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).setBuilderId(2).addExtra("extras", extras).build()) // 构建Android平台上的通知结构 .addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtra("extras", extras).build()); // 构建iOS平台上的通知结构 return notification.build(); } /**
* 发送模板短信
*
* @param phone 手机号
* @param params 模板变量
*/ public void sendTemplateSMS(String phone, Map<String, String> params) { sendTemplateSMS(phone, params, jPushConfig.getSmsTempId()); } /**
* 发送模板短信
*
* @param phone 手机号
* @param params 模板变量
* @param tempId 短信模板ID
*/ public void sendTemplateSMS(String phone, Map<String, String> params, int tempId) { log.info("开始发送模板短信"); Thread thread = new Thread(() -> { SMSPayload.Builder payload = SMSPayload.newBuilder(); payload.setMobileNumber(phone); payload.setTempId(tempId); for (Map.Entry<String, String> entry : params.entrySet()) { payload.addTempPara(entry.getKey(), entry.getValue()); } try { SMSClient client = new SMSClient(jPushConfig.getMasterSecret(), jPushConfig.getAppKey()); SendSMSResult res = client.sendTemplateSMS(payload.build()); log.info("SendSMSResult:{}", res); if (res.getResponseCode() == 200) { log.info("发送成功 手机号:" + phone + " 短信消息"); } else { log.info("发送失败 手机号:" + phone + " 短信消息"); } } catch (APIConnectionException e) { log.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { 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.getMessage()); } }); thread.start(); } /**
* 批量发送模板短信
*
* @param phoneList 手机号
* @param params 模板变量
* @param tempId 短信模板ID
*/ public void sendBatchTemplateSMS(List<String> phoneList, Map<String, String> params, int tempId) { log.info("开始批量发送模板短信"); List<RecipientPayload> list = new ArrayList<>(); for (String phone : phoneList) { RecipientPayload.Builder payload = RecipientPayload.newBuilder(); payload.setMobile(phone); for (Map.Entry<String, String> entry : params.entrySet()) { payload.addTempPara(entry.getKey(), entry.getValue()); } list.add(payload.build()); } RecipientPayload[] recipientPayloads = new RecipientPayload[list.size()]; BatchSMSPayload smsPayload = BatchSMSPayload.newBuilder() .setTempId(tempId) .setRecipients(list.toArray(recipientPayloads)) .build(); Thread thread = new Thread(() -> { long start = System.currentTimeMillis(); try { SMSClient client = new SMSClient(jPushConfig.getMasterSecret(), jPushConfig.getAppKey()); BatchSMSResult res = client.sendBatchTemplateSMS(smsPayload); log.info("BatchSMSResult:{}", res); for (String phone : phoneList) { log.info("手机号:" + phone + " 短信消息"); } } catch (APIConnectionException e) { log.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { 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.getMessage()); } log.info("批量发送模板短信,耗时" + (System.currentTimeMillis() - start) + "毫秒 "); }); thread.start(); } }
本文地址:https://blog.csdn.net/lishangqing/article/details/107874570
推荐阅读
-
SpringBoot整合MybatisPlus的简单教程实现(简单整合)
-
springboot+springmvc+mybatis项目整合
-
SpringMVC整合websocket实现消息推送及触发功能
-
SpringBoot中整合knife4j接口文档
-
SpringBoot缓存详解并整合Redis架构
-
springboot整合freemarker模板
-
详解SpringBoot开发案例之整合定时任务(Scheduled)
-
SpringBoot整合Elasticsearch7.2.0的实现方法
-
SpringBoot整合UEditor的示例代码
-
完整SpringBoot Cache整合redis缓存(二)