Spring Boot集成极光推送并且使用

pom.xml增加如下依赖:

        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.6.6</version>
        </dependency>

JPushUtil工具类如下:

@Slf4j
public class JPushUtil {
    protected static final Logger LOG = LoggerFactory.getLogger(JPushUtil.class);

    /** 设置好账号的app_key和masterSecret是必须的
          用自己创建以用后的APP_KEY和MASTER_SECRET
          替换即可,后期可写到yml配置文件中*/
    private static String APP_KEY = "xxxxxxxxxxxxxxxx";
    private static String MASTER_SECRET = "xxxxxxxxxxxxxxxxxxx";

    //根据别名推送消息,把房间号也推过去
    public static void pushMessageByAlias(List<String> aliases, CallRoomJPush callRoomJPush){
        ClientConfig clientConfig = ClientConfig.getInstance();
        final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
//        String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
        // Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
        // Call setHttpClient to set httpClient,
        // If you don't invoke this method, default httpClient will use NativeHttpClient.

//        ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
//        NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
//        jpushClient.getPushClient().setHttpClient(httpClient);

        // For push, all you need do is to build PushPayload object.
        String pushContentJson=new Gson().toJson(callRoomJPush);
        final PushPayload payload = buildPushMessage_android_and_ios(aliases,pushContentJson);

//        PushPayload payload = buildPushObject_all_alias_alert();
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
            System.out.println(result);
            // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
            // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
            // jpushClient.close();
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
            LOG.error("Sendno: " + payload.getSendno());

        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", 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());
        }
    }

    /**
     * Could modify the contents for pushing
     * The comments are showing how to use it
     * @return
     */
    public static PushPayload buildPushMessage_android_and_ios(List<String> aliases,String pushContentJson) {
        Map<String, String> extras = new HashMap<String, String>();
        extras.put("test", "https://community.jiguang.cn/push");
        // you can set anything you want in this builder, read the document to avoid collision.
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(aliases))
                .setMessage(Message.newBuilder()
                        .setMsgContent(pushContentJson)
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .setTimeToLive(43200)
                        .build())
                .build();
    }

    /**
     * Could modify the contents for pushing
     * The comments are showing how to use it
     * @return
     */
    public static PushPayload buildPushObject_android_and_ios() {
        Map<String, String> extras = new HashMap<String, String>();
        extras.put("test", "https://community.jiguang.cn/push");
        // you can set anything you want in this builder, read the document to avoid collision.
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
//                .setMessage(Message.newBuilder()
//                        .setMsgContent("Hi, JPush")
//                        .build())
                .setNotification(Notification.newBuilder()
                        .setAlert("testing alert content")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle("Android Title")
                                .addExtras(extras).build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("extra_key", "extra_value").build())
                        .build())
//                .setSMS(SMS.newBuilder()
//                        .setDelayTime(1000)
//                        .setTempID(2000)
//                        .addPara("Test", 1)
//                        .setActiveFilter(true)
//                        .build())
//                .setNotification3rd(Notification3rd.newBuilder()
//                        .setContent("Hi, JPush")
//                        .setTitle("msg testing")
//                        .setChannelId("channel1001")
//                        .setUriActivity("cn.jpush.android.ui.OpenClickActivity")
//                        .setUriAction("cn.jpush.android.intent.CONNECTION")
//                        .setBadgeAddNum(1)
//                        .setBadgeClass("com.test.badge.MainActivity")
//                        .setSound("sound")
//                        .addExtra("news_id", 124)
//                        .addExtra("my_key", "a value")
//                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .setTimeToLive(43200)
                        .build())
                .build();
    }

List类型的aliases是别名的集合,我们的项目里面把userId设置为了别名,所以aliases这个集合里面,存储的是要推送给哪些人的userId集合,而callRoomJPush是一个实体类,要往客户端传递的消息,这个根据自己的业务需要,去定义字段,最终客户端收到的消息里面的message字段,就是推送传过去的数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黄油奥特曼

如果可以,请我喝一杯咖啡

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

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

打赏作者

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

抵扣说明:

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

余额充值