1. 程式人生 > >極光消息推送多環境配置

極光消息推送多環境配置

true platform content illegal str leg result his 管理

在項目開發過程中,我們會使用到多個不同的環境開發、測試、生產,每個環境用來進行測試的打包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.測試推送

技術分享圖片

極光消息推送多環境配置