Java - springboot整合极光推送实战

 

1.添加依赖

<!--极光推送-->
<dependency>
	<groupId>cn.jpush.api</groupId>
	<artifactId>jpush-client</artifactId>
	<version>3.2.9</version>
</dependency>

2.添加配置类

JPushConfig

import cn.jpush.api.JPushClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

@Configuration
@ConfigurationProperties
public class JPushConfig {

    // 极光账号上的 AppKey
    @Value("7fd04ce01c973d1ad703bcb4")
    private String appkey;
    // 极光账号上的 Master Secret
    @Value("a8414209858d8037654258d0")
    private String secret;


    private JPushClient jPushClient;

    /**
     * 推送客户端
     * @return
     */
    @PostConstruct
    public void initJPushClient() {
        jPushClient = new JPushClient(secret, appkey);
    }

    /**
     * 获取推送客户端
     * @return
     */
    public JPushClient getJPushClient() {
        return jPushClient;
    }
}

 

 

3.推送实体

PushBean

import lombok.Data;
import java.util.Map;

@Data
public class PushBean {

    // 必填, 通知内容, 内容可以为空字符串,则表示不展示到通知栏。
    private String alert;
    // 可选, 附加信息, 供业务使用。
    private Map<String, String> extras;
    //android专用 可选, 通知标题	如果指定了,则通知里原来展示 App名称的地方,将展示成这个字段。
    private String title;
}

 

 

4.封装第三方API

IJPushService

import cn.jpush.api.push.model.PushPayload;

/**
 * 极光推送
 *
 * 封装第三方API
 */
public interface IJPushService {

    boolean pushAll(PushBean pushBean);

    boolean pushIos(PushBean pushBean);

    boolean pushIos(PushBean pushBean, String... registids);

    boolean pushAndroid(PushBean pushBean);

    boolean pushAndroid(PushBean pushBean, String... registids);

    boolean sendPush(PushPayload pushPayload);
}

JPushServiceImpl

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 极光推送
 *
 * 封装第三方API
 */
@Service
@Slf4j
public class JPushServiceImpl implements IJPushService {

    @Autowired
    private JPushConfig jPushConfig;

    /**
     * 广播 (所有平台,所有设备, 不支持附加信息)
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushAll(PushBean pushBean){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert(pushBean.getAlert()))
                .build());
    }

    /**
     * ios广播
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .build());
    }

    /**
     * ios通过registid推送 (一次推送最多 1000 个)
     * @param pushBean 推送内容
     * @param registids 推送id
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean, String... registids){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .build());
    }

    /**
     * android广播
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }

    /**
     * android通过registid推送 (一次推送最多 1000 个)
     * @param pushBean 推送内容
     * @param registids 推送id
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean, String ... registids){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }

    /**
     * 调用api推送
     * @param pushPayload 推送实体
     * @return
     */
    @Override
    public boolean sendPush(PushPayload pushPayload){
        log.info("发送极光推送请求: {}", pushPayload);
        PushResult result = null;
        try{
            result = jPushConfig.getJPushClient().sendPush(pushPayload);
        } catch (APIConnectionException e) {
            log.error("极光推送连接异常: ", e);
        } catch (APIRequestException e) {
            log.error("极光推送请求异常: ", e);
        }
        if (result!=null && result.isResultOK()) {
            log.info("极光推送请求成功: {}", result);
            return true;
        }else {
            log.info("极光推送请求失败: {}", result);
            return false;
        }
    }
}

 

 

5.封装业务功能

IMyJPushService

/**
 * 推送服务
 *
 * 封装业务功能
 */
public interface IMyJPushService {

    boolean pushAll(PushBean pushBean);

    boolean pushIos(PushBean pushBean);

    boolean pushIos(PushBean pushBean, String... registids);

    boolean pushAndroid(PushBean pushBean);

    boolean pushAndroid(PushBean pushBean, String... registids);

    String[] checkRegistids(String[] registids);
}

MyJPushServiceImpl

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 推送服务
 *
 * 封装业务功能
 */
@Service
public class MyJPushServiceImpl implements IMyJPushService {

    /** 一次推送最大数量 (极光限制1000) */
    private static final int max_size = 800;

    @Autowired
    private IJPushService jPushService;

    /**
     * 推送全部, 不支持附加信息
     * @return
     */
    @Override
    public boolean pushAll(PushBean pushBean){
        return jPushService.pushAll(pushBean);
    }

    /**
     * 推送全部ios
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean){
        return jPushService.pushIos(pushBean);
    }

    /**
     * 推送ios 指定id
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean, String... registids){
        registids = checkRegistids(registids); // 剔除无效registed
        while (registids.length > max_size) { // 每次推送max_size个
            jPushService.pushIos(pushBean, Arrays.copyOfRange(registids, 0, max_size));
            registids = Arrays.copyOfRange(registids, max_size, registids.length);
        }
        return jPushService.pushIos(pushBean, registids);
    }

    /**
     * 推送全部android
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean){
        return jPushService.pushAndroid(pushBean);
    }

    /**
     * 推送android 指定id
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean, String... registids){
        registids = checkRegistids(registids); // 剔除无效registed
        while (registids.length > max_size) { // 每次推送max_size个
            jPushService.pushAndroid(pushBean, Arrays.copyOfRange(registids, 0, max_size));
            registids = Arrays.copyOfRange(registids, max_size, registids.length);
        }
        return jPushService.pushAndroid(pushBean, registids);
    }

    /**
     * 剔除无效registed
     * @param registids
     * @return
     */
    @Override
    public String[] checkRegistids(String[] registids) {
        List<String> regList = new ArrayList<String>(registids.length);
        for (String registid : registids) {
            if (registid!=null && !"".equals(registid.trim())) {
                regList.add(registid);
            }
        }
        return regList.toArray(new String[0]);
    }
}

 

 

6.测试 - 广播

JPushController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class JPushController {

    @Autowired
    private IMyJPushService service;

    /**
     * 广播
     * @param title
     * @param content
     * @param registids  指定id
     * @return
     */
    @PostMapping("/jpush")
    @ResponseBody
    public boolean test(String title, String content, String registids){
        PushBean pushBean = new PushBean();
        pushBean.setTitle(title);
        pushBean.setAlert(content);
        return service.pushIos(pushBean);
    }
}

发送给所有ios用户

温馨提示: 指定id可以发给某个ios用户, android的道理也是一样, 我这里就不一 一演示了。大家动起手来实战一下...

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,下面是SpringBoot集成极光推送进行推送消息的步骤和代码示例: 1. 引入极光推送Java-SDK的依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>cn.jiguang</groupId> <artifactId>jiguang-common</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>cn.jiguang</groupId> <artifactId>jiguang-api</artifactId> <version>1.3.0</version> </dependency> ``` 2. 配置极光推送的AppKey和MasterSecret 在SpringBoot的配置文件中添加如下配置: ```properties jpush.appKey=yourAppKey jpush.masterSecret=yourMasterSecret ``` 3. 编写推送消息的代码 在需要推送消息的地方,注入JPushClient对象,然后调用JPushClient的相关方法即可实现推送消息的功能。以下是一个简单的推送消息的示例: ```java @RestController public class PushController { @Autowired private JPushClient jPushClient; @PostMapping("/push") public String pushMessage(@RequestParam("message") String message) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.all()) .setNotification(Notification.alert(message)) .build(); try { PushResult result = jPushClient.sendPush(payload); return result.toString(); } catch (APIConnectionException | APIRequestException e) { e.printStackTrace(); return e.getMessage(); } } } ``` 在上述代码中,我们构建了一个PushPayload对象,设置了推送平台、推送对象和推送消息,然后调用JPushClient的sendPush方法发送推送消息。 以上就是SpringBoot集成极光推送进行推送消息的基本步骤和代码示例。希望能够对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_43652507

谢谢打赏,祝老板心想事成

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

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

打赏作者

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

抵扣说明:

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

余额充值