Springboot 集成极光推送

前提

之前接触的极光推送都是关于移动端的推送,服务器端的推送没有接触过,另外关于Spring boot,如何集成也是一知半解,没有一个完整的概念,期间也踩了坑。另外就是网上的文章和官方文档说的不清不楚的,搞得就更懵了。但是,总归完成了,所以写篇文章记录一下。

第一步

接入前的准备

  • 1、pom.xml添加极光依赖

     <dependency>
          <groupId>cn.jpush.api</groupId>
          <artifactId>jpush-client</artifactId>
          <version>3.4.6</version>
      </dependency>
    
  • 2、添加解析 application.yaml依赖

    <!-- 解析yml -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <optional>true</optional>
      </dependency>
    
  • 3、在application.yaml添加自定义参数(如下图所示)

    jpush:
      app-key: 14edd0c2977ac5cb856bc622
      master-secret: 3a3279e47e70065e04c67329
    
image.png

第二步

参数读取和代码编写

  • 1、新建JPushParam类读取application.yaml中的app-keymaster-secret

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Component
    @ConfigurationProperties(prefix = "jpush")
    public class JPushParam {
    
        private String app_key;
        private String master_secret;
    
    }
    
  • 2、创建JPushManager类,用于创建推送主体

    @Component
    public class JPushManager {
    
    
        @Autowired
        private JPushParam param;
    
    
    
       /**
        * 发送给所有Android用户
        */
      public PushResult sendToAndroid(String content, String title, Map<String, String> extras) {
          JPushClient pushClient = new JPushClient(param.getMaster_secret(),   param.getApp_key());
          PushPayload payload = PushPayload.newBuilder()
              .setPlatform(Platform.android())
              .setAudience(Audience.all())
              .setNotification(Notification.android(content, title, extras))
              .build();
      try {
          return pushClient.sendPush(payload);
      } catch (APIConnectionException | APIRequestException e) {
          e.printStackTrace();
      }
      return null;
    
      }
    
      /**
       * 发送给所有Ios用户
       */
    public PushResult sendToIos(Object alert, Map<String, String> extras) {
      JPushClient pushClient = new JPushClient(param.getMaster_secret(), param.getApp_key());
      PushPayload payload = PushPayload.newBuilder()
              .setPlatform(Platform.ios())
              .setAudience(Audience.all())
              .setNotification(Notification.ios(alert, extras))
              .build();
      try {
          return pushClient.sendPush(payload);
      } catch (APIConnectionException | APIRequestException e) {
          e.printStackTrace();
      }
      return null;
      }
    
      /**
       * 发送给所有用户
       */
    public PushResult sendToAll(Object alert) {
      JPushClient pushClient = new JPushClient(param.getMaster_secret(), param.getApp_key());
      PushPayload payload = PushPayload.newBuilder()
              .setPlatform(Platform.all())
              .setAudience(Audience.all())
              .setNotification(Notification.alert(alert))
              .build();
      try {
          return pushClient.sendPush(payload);
      } catch (APIConnectionException | APIRequestException e) {
          e.printStackTrace();
      }
      return null;
      }
    }
    
  • 3、创建PushController类调用接口发送推送(当然也有其他方式触发,请根据实际情况自行处理,此处仅为了测试推送

    @RestController
    @RequestMapping("/push")
    @Api("PushController Api 接口文档")// @Api:修饰整个类,描述Controller的作用
    public class PushController {
    
    
      @Autowired
      private JPushManager manager;
    
      /**
       * 发送
       */
      @ApiOperation(value = "发送推送", notes = "发送推送")
      @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")
      @PostMapping("/sendMsg")
      public Message<String> sendMsg() {
          Map<String, String> map = new WeakHashMap<>();
          PushResult result =  manager.sendToAndroid("风萧萧兮易水寒", "通知", map);
          return MessageUtil.success(result.toString());
      }
    }
    
  • 4、客户端调用

    image.png

    注意:因为我这里是把发送过的用户ID保存到数据库中的,所以会显示已经发送过,并且今天发送的广播数量超过极光给的数量了,所以今天不能发送了。但是,上面的代码都是没有问题的,请放心使用,其他自定义发送的方式请自行实现。

最后

好了,这就是今天关于Spring boot 集成极光文档的全部内容,上面的代码就是我所使用的全部内容,可以自行复制然后加以修改使用。如果还有其他问题,欢迎进水群(493180098)探讨。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。