Springboot集成极光推送

极光链接:
官网:https://www.jiguang.cn
文档:https://docs.jiguang.cn//jpush/guideline/intro/
java示例git地址:https://github.com/jpush/jpush-api-java-client

#1.pom.xml导入依赖

        <!-- 极光推送 -->
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.4.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bcprov-jdk15on</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.bouncycastle</groupId>
                    <artifactId>bcpkix-jdk15on</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

#2.yml配置

jiguang:
  appKey: 5a9**************48c
  masterSecret: e***********6a

3.代码,通知消息

public static PushResult sendPushForPlatform(PushConfig cfg, PushMsgBO pushMsgBO)
        throws APIRequestException {
        Map<String, String> extras = getExtrasMap(cfg, pushMsgBO);
        ClientConfig clientConfig = ClientConfig.getInstance();
        final JPushClient jpushClient = new JPushClient(cfg.getMasterSecret(), cfg.getAppKey(), null, clientConfig);
        final PushPayload payload = buildPush(cfg, pushMsgBO.getAlert(), pushMsgBO.getTitle(), extras,
            pushMsgBO.getTarget(), pushMsgBO.getTag(), pushMsgBO.getAlias());
        try {
            PushResult result = jpushClient.sendPush(payload);
            log.info("Got result - " + result);
            return result;
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
            log.error("Sendno: " + payload.getSendno());
            throw new GlobalException("推送消息失败");
        } catch (APIRequestException 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());
            throw e;
        }
    }

#4.构建消息体

  /**
     * 构建全部安卓与推送消息
     *
     * @param alert 弹出信息
     * @param title 消息标题
     * @param extras 消息体
     */
    public static PushPayload buildPush(PushConfig cfg, String alert, String title,
        Map<String, String> extras, String target, String tag, List<String> alias) {
        //设置ios与安卓消息提
        IosNotification.Builder ios_builder = IosNotification.newBuilder()
            .incrBadge(1)
            .setSound("default")
            .setAlert(IosAlert.newBuilder()
                .setTitleAndBody(title, "", alert)
                .build());
        //自定义参数
        extras.entrySet().forEach(con -> {
            ios_builder.addExtra(con.getKey(), con.getValue());
        });
        AndroidNotification.Builder android_builder = AndroidNotification.newBuilder()
            .setTitle(title)
            .addExtras(extras)
            .setAlert("")//安卓alert有值时,会出现2条通知
            .setLargeIcon(cfg.getLargeIcon());//设置icon图标
        //if (PushTargetEnum.IOS.getId().equals(target)) {
        return getIosBuild(cfg, ios_builder, alias, tag);
        } else if (PushTargetEnum.ANDROID.getId().equals(target)) {
            return getAndroidBuild(cfg, android_builder,alias,tag);
        } else {
            return getAndroidIosBuild(cfg, ios_builder, android_builder,alias,tag);
        }
    }


#5.ios配置
安卓配置与ios设置类似。

   private static PushPayload getIosBuild(PushConfig cfg, Builder ios_builder, List<String> alias, String tag) {
        Audience audience = getAudience(alias, tag, cfg);
        return PushPayload.newBuilder()
            .setPlatform(Platform.ios())//设置平台
            .setAudience(audience)//设置根据别名、标识、所有推送(Audience.tag("xxx"):标识;Audience.alias("xxx"):别名;Audience.all():所有)
            .setOptions(Options.newBuilder()
                .setApnsProduction(cfg.getIsProd())//设置推送环境,测试与正式环境
                .setSendno(ServiceHelper.generateSendno())
                .build())
            .setNotification(Notification.newBuilder()
                .addPlatformNotification(ios_builder.build())//设置消息体
                .build())
            .build();
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LC超人在良家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值