在项目开发过程中,我们会使用到多个不同的环境开发、测试、生产,每个环境用来进行测试的打包APP也不一样,我们需要增加不同的环境配置,去满足需求。
我是基于Maven管理的SpringBoot项目
1.涉及到的环境有开发环境+测试环境+生产环境
2.在极光服务器增加应用配置
添加三个环境的应用配置
每个应用中获取AppKey和MasterSecret
将变量放到配置文件中
如下图所示
3.从配置文件中读取环境信息
@Configuration public class JPushClientParamsModel { //极光用于标识唯一应用 private String appKey; //客户端给极光推送时验证 private String masterSecret; //IOS设备是否推生产环境 private String apnsProdFlg; public String getAppKey() { return appKey; } @Value("${jpush.project.appkey}") public void setAppKey(String appKey) { this.appKey = appKey; } public String getMasterSecret() { return masterSecret; } @Value("${jpush.project.masterkey}") public void setMasterSecret(String masterSecret) { this.masterSecret = masterSecret; } public String getApnsProdFlg() { return apnsProdFlg; } @Value("${jpush.project.ios.apnsProdFlg}") public void setApnsProdFlg(String apnsProdFlg) { this.apnsProdFlg = apnsProdFlg; } }
4.创建全局静态的JPushClient
@Component public class PhiPushComponent { private static final Logger logger = LogManager.getLogger(PhiPushComponent.class); private static String MASTER_SECRET = "";// JPush服务器端下发的master_key private static String APP_KEY = "";// JPush服务器端下发的app_key private static String APNS_PRODFLG = "";// 是否发送到IOS设备的生产环境 private static JPushClient jpushClient = null; @Autowired private JPushClientParamsModel jpushClientParamsModel; /** * 创建统一的JPush请求客户端 */ @PostConstruct public void initJPushClient() { //读取配置文件中的masterSecret MASTER_SECRET = jpushClientParamsModel.getMasterSecret(); //读取配置文件中的appKey APP_KEY = jpushClientParamsModel.getAppKey(); //读取配置文件中的apnsFlg APNS_PRODFLG = jpushClientParamsModel.getApnsProdFlg(); //创建全局统一的JPushClient jpushClient = new JPushClient(jpushClientParamsModel.getMasterSecret(), jpushClientParamsModel.getAppKey(), null, ClientConfig.getInstance()); } /** * 根据上报的设备类型,选择推送 * @param model 上报设备的设备类型 1.Android 2.IOS 3.WinPhone 4.其他 * @return */ public JPushResponse jpushWithRegistId(PushWithRegistIdRequestModel model) { JPushResponse rsp = new JPushResponse(); //设备类型为空时直接返回 if(StringUtil.isNullOrEmpty(model.getOsType())){ rsp.setStatusCode(Const.ResponseStatus.STATUS_NO_OS_TYPE_IN_REQUEST); return rsp; } if(model.getOsType().equals("0")){ rsp = jPushToAndroid(model); rsp = jPushToiOS(model); }else if(model.getOsType().equals("1")){ rsp = jPushToAndroid(model); }else if(model.getOsType().equals("2")){ rsp = jPushToiOS(model); }else{ rsp.setStatusCode(Const.ResponseStatus.STATUS_ILLEGAL_OS_TYPE_IN_REQUEST); } return rsp; } private JPushResponse jPushToAndroid(PushWithRegistIdRequestModel model) { JPushResponse rsp = new JPushResponse(); try { PushResult result = jpushClient.sendAndroidMessageWithRegistrationID(model.getTitle(), model.getMessage(), model.getRegistrationId()); rsp.setStatusCode(result.getResponseCode()); } catch (APIConnectionException e) { logger.error(e); rsp.setStatusCode(10001); } catch (APIRequestException e) { logger.error(e); rsp.setStatusCode(10002); } logger.debug("Push to android rsp [{}]", rsp.getStatusCode()); return rsp; } private JPushResponse jPushToiOS(PushWithRegistIdRequestModel model) { JPushResponse rsp = new JPushResponse(); PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios()) .setAudience(Audience.newBuilder() .addAudienceTarget(AudienceTarget.registrationId(model.getRegistrationId())).build()) .setMessage( Message.newBuilder().setMsgContent(model.getMessage()).addExtra("from", "ProjectPush").build()) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder().setAlert(model.getMessage()).setBadge(0) .setSound("default").build()) .build()) .setOptions( Options.newBuilder().setApnsProduction(Boolean.parseBoolean(APNS_PRODFLG)).build()) .build(); //统一使用全局唯一的发送客户端 pushInner(rsp, jpushClient, payload); logger.debug("Push to iOS rsp [{}]", rsp.getStatusCode()); return rsp; } }
5.测试推送