300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > SpringBoot 自定义Starter(阿里云短信 消息推送)

SpringBoot 自定义Starter(阿里云短信 消息推送)

时间:2019-10-10 01:19:42

相关推荐

SpringBoot 自定义Starter(阿里云短信 消息推送)

首先在IDEA中创建SpringBoot项目,引入相关必要依赖,本次以阿里云短信/消息推送为例:

<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.4.6</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-push</artifactId><version>3.9.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.3.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.3.0.RELEASE</version><scope>compile</scope></dependency>

在resources目录下创建META-INF文件夹,并在该文件下创建spring.factories文件,文件内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.xxxxx.aliyun.config.AliYunMessageAutoConfiguration,\com.xxxxx.aliyun.config.AliYunPushMessageAutoConfiguration

该文件内配置的是自定义starter的路径,配置了两个自定义starter,分别为阿里云短信、阿里云消息推送的starter。

阿里云短信:

AliYunMessageProperties.java

@Getter@Setter@ConfigurationProperties(prefix = "aliyun.sms")public class AliYunMessageProperties {private Boolean enable = false;private String accessKeyId;private String accessKeySecret;private String regionId = "cn-hangzhou";private String signName;}

AliYunMessageAutoConfiguration.java

@Configuration@RequiredArgsConstructor@EnableConfigurationProperties(AliYunMessageProperties.class)@ConditionalOnProperty(prefix = "aliyun.sms", value = "enable", havingValue = "true")public class AliYunMessageAutoConfiguration {private final AliYunMessageProperties aliYunMessageProperties;@Beanpublic AliYunSendMessage aliYunSendMessage(){return new AliYunSendMessage(aliYunMessageProperties.getRegionId(), aliYunMessageProperties.getAccessKeyId(),aliYunMessageProperties.getAccessKeySecret(), aliYunMessageProperties.getSignName());}}

AliYunSendMessage.java

@Slf4j@AllArgsConstructorpublic class AliYunSendMessage {public String regionId;public String accessKeyId;public String accessSecret;public String signName;/**** @param phoneNum 发送手机* @param templateCode 短信模板code* @param templateParam 短信内容参数JSON格式* @return*/public String sendMessage(String phoneNum, String templateCode, String templateParam){DefaultProfile profile = DefaultProfile.getProfile(this.regionId, this.accessKeyId, this.accessSecret);IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);request.setSysDomain("");request.setSysVersion("-05-25");request.setSysAction("SendSms");request.putQueryParameter("RegionId", this.regionId);request.putQueryParameter("PhoneNumbers", phoneNum);request.putQueryParameter("SignName", this.signName);request.putQueryParameter("TemplateCode", templateCode);request.putQueryParameter("TemplateParam", templateParam);try {CommonResponse response = client.getCommonResponse(request);log.info("response data : {}", response.getData());return response.getData();} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return null;}}

完成上述内容后就可以使用该自定义的starter了,在使用的模块引入该自定义starter。

使用该starter模块的application.yml配置文件需填入如下内容:

aliyun:sms:accessKeyId: xxxxx #阿里云keyaccessKeySecret: xxxxx #阿里云secretregionId: xxxxx #阿里云配置地区Id 通常为cn-hangzhousignName: xxxxx #配置xxx, 阿里云控制台中获取enable: true

阿里云消息推送:

MobileDeviceEnums.java

@Getter@AllArgsConstructorpublic enum MobileDeviceEnums {ANDROID(1, "ANDROID"),IOS(2, "IOS");private final Integer code;private final String desc;}

AliYunPushMessageProperties.java

@Getter@Setter@ConfigurationProperties(prefix = "aliyun.push")public class AliYunPushMessageProperties {private Boolean enable = false;private String accessKeyId;private String accessKeySecret;private String regionId = "cn-hangzhou";private String appKeyAndroid;private String appKeyIOS;}

AliYunPushMessageAutoConfiguration.java

@Configuration@RequiredArgsConstructor@EnableConfigurationProperties(AliYunPushMessageProperties.class)@ConditionalOnProperty(prefix = "aliyun.push", value = "enable", havingValue = "true")public class AliYunPushMessageAutoConfiguration {private final AliYunPushMessageProperties aliYunPushMessageProperties;@Bean@ConditionalOnMissingBeanpublic AliYunPushMessage aliYunPushMessage(){return new AliYunPushMessage(aliYunPushMessageProperties.getRegionId(), aliYunPushMessageProperties.getAccessKeyId(), aliYunPushMessageProperties.getAccessKeySecret(),aliYunPushMessageProperties.getAppKeyAndroid(), aliYunPushMessageProperties.getAppKeyIOS());}}

AliYunPushMessage.java

@Slf4j@AllArgsConstructorpublic class AliYunPushMessage {public String regionId;public String accessKeyId;public String accessSecret;public String appKeyAndroid;public String appKeyIOS;/**** @param deviceTypeEnum 设备类型* @param userId 阿里云推送别名* @param title 消息头* @param body 消息体* @param extParam 扩展参数* @param activity 安卓activity地址 (如: com.zhongzhi.ui.home.activity.ActivityOrderInfo)*/public void pushMessage(MobileDeviceEnums deviceTypeEnum, String userId, String title, String body, String extParam, String activity){DefaultProfile profile = DefaultProfile.getProfile(this.regionId, this.accessKeyId, this.accessSecret);IAcsClient client = new DefaultAcsClient(profile);PushRequest pushRequest = new PushRequest();switch (deviceTypeEnum) {case IOS:// 推送目标。pushRequest.setAppKey(Long.valueOf(this.appKeyIOS));pushRequest.setTarget("ACCOUNT"); //推送目标。device:推送给设备; account:推送给指定帐号,tag:推送给自定义标签; all:推送给全部。pushRequest.setTargetValue(userId);pushRequest.setPushType("NOTICE"); // 消息类型MESSAGE NOTICE。pushRequest.setDeviceType("iOS"); // 设备类型ANDROID iOS ALL。// 推送配置。pushRequest.setTitle(title); // 消息的标题。pushRequest.setBody(body); // 消息的内容。pushRequest.setIOSApnsEnv("PRODUCT");pushRequest.setIOSExtParameters(extParam);// 推送配置:Android。//pushRequest.setAndroidNotifyType("BOTH");//通知的提醒方式。"VIBRATE":震动、"SOUND":声音、"BOTH":声音和震动、NONE:静音//pushRequest.setAndroidOpenType("APPLICATION"); //点击通知后动作。"APPLICATION":打开应用、"ACTIVITY":打开AndroidActivity、"URL":打开URL、"NONE":无跳转。// 指定notificaitonchannel id。break;case ANDROID:// 推送目标。pushRequest.setAppKey(Long.valueOf(this.appKeyAndroid));pushRequest.setTarget("ACCOUNT"); //推送目标。device:推送给设备; account:推送给指定帐号,tag:推送给自定义标签; all:推送给全部。pushRequest.setTargetValue(userId);pushRequest.setPushType("NOTICE"); // 消息类型MESSAGE NOTICE。pushRequest.setDeviceType("ANDROID"); // 设备类型ANDROID iOS ALL。// 推送配置。pushRequest.setTitle(title); // 消息的标题。pushRequest.setBody(body); // 消息的内容。pushRequest.setAndroidOpenType("ACTIVITY");pushRequest.setAndroidActivity(activity);//设置辅助推送配置pushRequest.setAndroidPopupActivity(activity);pushRequest.setAndroidPopupTitle(title);pushRequest.setAndroidPopupBody(body);// 离线消息是否保存,若保存, 在推送时候,用户即使不在线,下一次上线则会收到pushRequest.setStoreOffline(true);//推送消息类型时,设置true,设备离线时会自动把消息转成辅助通道的通知pushRequest.setAndroidRemind(true);// 72小时后消息失效, 不会再发送String expireTime = ParameterHelper.getISO8601Time(new Date(System.currentTimeMillis() + 72 * 3600 * 1000));pushRequest.setExpireTime(expireTime);pushRequest.setAndroidExtParameters(extParam);// 推送配置:Android。// 指定notificaitonchannel id。//pushRequest.setAndroidNotificationChannel("zhongzhi_android");break;default:break;}try {PushResponse response = client.getAcsResponse(pushRequest);log.info("push response : {}", new Gson().toJson(response));} catch (ClientException e) {e.printStackTrace();}}}

同样完成上述内容,使用该starter模块的application.yml配置文件需填入如下内容:

aliyun:push:accessKeyId: xxxxx accessKeySecret: xxxxxxregionId: cn-hangzhou #阿里云默认配置appKeyAndroid: xxxxxappKeyIOS: xxxxxx

源码参考GitHub链接:GitHub - liaozuyao/aliyun-sms-push

其他starter:微信公众号消息推送GitHub:GitHub - liaozuyao/wechat-message-starter

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。