极光推送

这里使用别名方式推送!!!
Andriod和IOS同步使用别名形式!
必要参数:
PUSHCLIENTPRO_KEY=598ffcxxxxx1c0c
PUSHCLIENTPRO_MASTERSECRET=c373bdaxxxxxx3a008
1.引入极光推送jar



    cn.jpush.api
    jpush-client
    3.4.3

2.自定义工具类

@Slf4j
@Service
@Transactional
public class JPushSendMessageServiceImpl implements JPushSendMessageService{

    @Value("${spring.profiles.active:}")
    private String PUBLISH_EVN;
    @Value("${ssx.jpush.apnsProduction}")
    private boolean APNSPRODUCTION;

    private static final String PUSHCLIENTPRO_KEY = "598fxxxxc1c0c";
    private static final String PUSHCLIENTPRO_MASTERSECRET = "c373xxxx3a008";

    @Override
    public int jpush(JPushCommonParam param) throws APIConnectionException, APIRequestException {
        PushResult pushResult;
        HttpProxy proxy = null;
        // 非dev环境,使用代理进行测试
        if (Arrays.asList("test", "run").contains(PUBLISH_EVN)) {
            proxy= new HttpProxy(HttpConfigure.getProxySslIp(), HttpConfigure.getProxySslPort());
            log.info("========== 代理服务器地址: {}", JSON.toJSONString(proxy.getNetProxy().address()));
        }
        Platform platform = Platform.all();
        Options options = Options.newBuilder().setApnsProduction(APNSPRODUCTION).build();
        //android通知
        AndroidNotification androidNotification =
                AndroidNotification.newBuilder()
                        .setAlert(param.getContent())
                        .setTitle(param.getTitle())
                        .addExtra("bid", param.getBid())
                        .build();
        //ios通知
        IosAlert alert = IosAlert.newBuilder()
                .setTitleAndBody(param.getTitle(),null,param.getContent())
                .setActionLocKey("PLAY")
                .build();
        IosNotification iosNotification = IosNotification.newBuilder()
                .setAlert(alert)
                .setSound("default")
                .setBadge(+1)
                .addExtra("bid", param.getBid())
                .addExtra("btype", btype)
                .build();
        //设置别名
        Audience audience;
        if(param.getRegistraidList() != null && !param.getRegistraidList().isEmpty()){
            List userCodeAndActive = new ArrayList<>();
            param.getRegistraidList().stream().forEach(x->{
                userCodeAndActive.add(x + "_" + PUBLISH_EVN);
            });
            audience = Audience.alias(userCodeAndActive);
        }else{
            audience = Audience.all();//广播
        }
        Notification notification = Notification
                .newBuilder()
                .addPlatformNotification(androidNotification)
                .addPlatformNotification(iosNotification)
                .build();
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(platform)
                .setAudience(audience)
                .setOptions(options)
                .setNotification(notification)
                .build();
        JPushClient pushClient = new JPushClient(PUSHCLIENTPRO_MASTERSECRET, PUSHCLIENTPRO_KEY,proxy, ClientConfig.getInstance());
        try {
            log.info("========【开始推送】input:{}",JSON.toJSONString(pushPayload));
            pushResult = pushClient.sendPush(pushPayload);
            if(pushResult.getResponseCode() == 200){
                log.info("=======【推送成功】input:{}",pushResult.getResponseCode());
                return 1;
            }
        } catch (APIConnectionException e) {
            log.info("=======【推送失败】sendPush APIConnectionException: "+e.getMessage());
        } catch (APIRequestException e) {
            log.info("=======【推送失败】sendPush APIRequestException: "+e.getMessage());
        } catch (Exception e){
            log.info("=======【推送失败】sendPush exception: "+e.getMessage());
        }
        return 0;
    }
}

3.准备参数-推送

JPushCommonParam jPushCommonParam = new JPushCommonParam();
//设置业务
jPushCommonParam.setBid(mCode);
jPushCommonParam.setTitle(pubNumberMatter.getTitle());
jPushCommonParam.setRegistraidList(userCodeList);
jPushCommonParam.setContent(pubNumberMatter.getTabloid());
jPushSendMessageService.jpush(jPushCommonParam)

你可能感兴趣的:(java,极光别名推送)