极光推送 + MQ

将 极光推送 放到mq队列中,mq有几个注解  都是配合使用的 最简单的两个
@RabbitListener(queues = “   ”,containerFactory="rabbitListenerContainerFactory")

@RabbitHandler

在方法中一定要有异常捕捉 ,要不然会陷入死循环,通过异常来跳出队列发送

调用极光的方法

@Component

@RabbitListener(queues = JPushServer.JPUSH_MQ_NAME,containerFactory="rabbitListenerContainerFactory")

public class JPushServer {

public static final StringJPUSH_MQ_NAME ="JPUSH_MQ_NAME";

  public static final String APPKEY ="appKey";

  public static final String MASTERSECRET =" MASTERSECRET ";

  public static JPushClientjpushClient =null;

  @RabbitHandler

  void getJPush(JPushModel jPushModel){

try {

//推送目标 为空时全部推送

        if(jPushModel.getObject() ==null){

sendPush(jPushModel.getContent(),jPushModel.getTitle(),jPushModel.getParams());

        }else{

//推送目标 String类型时单体推送

            if(jPushModel.getObject().getClass().isArray()){

sendByAlias(jPushModel.getContent(),jPushModel.getTitle(),jPushModel.getParams(),(String) jPushModel.getObject());

            }else if(jPushModel.getObject()instanceof String){//推送目标  List类型时选择性推送

              sendByAliasList(jPushModel.getContent(),jPushModel.getTitle(),jPushModel.getParams(),(List) jPushModel.getObject());

            }

}

Thread.sleep(5000);

        jpushClient.close();

      }catch (InterruptedException e) {

e.printStackTrace();

      }

}

/**

    * 功能描述:

    * 〈全平台推送〉

    * @Param: [msg, title, params]

    * @Return: void

    * @Author:  

    * @Date: 2019/8/14 14:10

*/

  public static void sendPush(String msg,String title, Map params) {

jpushClient =new JPushClient(MASTERSECRET, APPKEY);

      PushPayload payload =buildPushObjectByMessage(msg,title,params);

      try {

PushResult result =jpushClient.sendPush(payload);

      }catch (Exception e) {

e.printStackTrace();

      }

}

public static PushPayloadbuildPushObjectByMessage(String content,String title, Map params) {

PushPayload.Builder builder = PushPayload.newBuilder();

      builder.setPlatform(Platform.all())// 设置接受的平台

            .setAudience(Audience.all());// Audience设置为all,说明采用广播方式推送,所有用户都可以接收到

      Notification.Builder notificationBuilder = Notification.newBuilder();

      JPushServer.notificationBuilder(notificationBuilder,content,title,params);

      builder.setNotification(notificationBuilder.build());

      return builder.build();

  }

/**

    * 功能描述:

    * 〈根据别名推送〉

    * @Param: [content, title, params, userId]

    * @Return: void

    * @Author:  

    * @Date: 2019/8/14 14:17

*/

  public static void sendByAlias(String content,String title, Map params, String userId) {

ClientConfig clientConfig = ClientConfig.getInstance();

      JPushClient jpushClient =new JPushClient(MASTERSECRET, APPKEY, null, clientConfig);

      List users =new ArrayList();

      users.add(userId);

      PushPayload payload =buildPushObjectByAlias(content,title, params, users);

      try {

PushResult result = jpushClient.sendPush(payload);

      }catch (APIConnectionException e) {

}catch (APIRequestException e) {

}

}

/**

    * 功能描述:

    * 〈根据别名集合发送〉

    * @Param: [content, title, params, users]

    * @Return: void

    * @Author:  

    * @Date: 2019/8/14 14:18

*/

  public static void sendByAliasList(String content,String title, Map params,List users) {

ClientConfig clientConfig = ClientConfig.getInstance();

      JPushClient jpushClient =new JPushClient(MASTERSECRET, APPKEY, null, clientConfig);

      PushPayload payload =buildPushObjectByAlias(content, title,params, users);

      try {

PushResult result = jpushClient.sendPush(payload);

      }catch (APIConnectionException e) {

}catch (APIRequestException e) {

}

}

private static PushPayloadbuildPushObjectByAlias(String content,String title, Map params, List userIds) {

PushPayload.Builder builder = PushPayload.newBuilder();

      builder.setPlatform(Platform.all());

      builder.setAudience(Audience.alias(userIds));

      Notification.Builder notificationBuilder = Notification.newBuilder();

      JPushServer.notificationBuilder(notificationBuilder,content,title,params);

      builder.setNotification(notificationBuilder.build());

      return builder.build();

  }

public static void notificationBuilder(Notification.Builder notificationBuilder,String content,String title, Map params){

notificationBuilder.setAlert(content);

      notificationBuilder.addPlatformNotification(IosNotification.newBuilder().setAlert(content).setSound("happy").addExtras(params).build());

      notificationBuilder.addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).setAlert(content).addExtras(params).build());

  }

public static void main(String[] args) {

Map params =new HashMap();

      //map里面可以放一些你需要展示的或者你需要传递的数据

      params.put("type", "1");

      sendByAlias("(即将作业)您的作业将于2小时后开始","测试标题", params,"1" );

  }

}

调用推送

/**

* 功能描述:

* 〈您好,您申请的巴拿马籍杂货船“豪兴1号”离金海码头6号泊位的引航申请,主引XXX变更为XXX,负责本次引航任务。〉

* @Param: [jPushRequest]

* @Return: void

* @Author:  

* @Date: 2019/8/14 16:35

*/

public void agentChangePilot(JPushRequest jPushRequest){

String content ="您好,您申请的"+jPushRequest.getNationality()+"籍"+jPushRequest.getShipType()+

"“"+jPushRequest.getShipName()+

"”"+jPushRequest.getJobType()+jPushRequest.getAimName()+"{4}的引航申请,主引{5}变更为{6},负责本次引航任务。";

    getData(content,new JPushModel(),jPushRequest);

}

public void getData(String content,JPushModel jPushModel,JPushRequest jPushRequest){

jPushModel.setContent(content);

    jPushModel.setParams(jPushRequest.getParams());

    jPushModel.setObject(jPushRequest.getObject());

    jPushModel.setTitle(jPushRequest.getTitle());

    rabbitTemplate.convertAndSend(JPushServer.JPUSH_MQ_NAME,jPushModel);

}

你可能感兴趣的:(极光推送 + MQ)