springboot 极光消息推送

1.pom.xml

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

2.JpushUtils.java

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
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.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

public class JpushUtils{
   protected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);

   protected static final String APP_KEY ="xxxxxx";
   protected static final String MASTER_SECRET = "xxxxxxx";
   /**
    * 提醒tag
    **/
   public static final String NOTICE_TAG   = "20";
   /**
    * 标题
    **/
   public static final String NOTICE_TITLE = "xxxx提醒";
   /**
    * 消息体
    **/
   public static final String NOTICE_MSG   = "xxxx{0}待确认,请及时处理!";

   /**
    * @Description: 发送app极光消息
    * @Author: duyaqiong
    * @Date: 2020/9/11 16:11
    * @Return: void
    **/
   public static void sendJGNotice(String mobile,String orderNo)
   {
      //创建JPushClient
      JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
      // 自定义参数
      Map<String, String> params = new HashMap<String, String>();
      params.put("orderNo",orderNo);

      // 必须参数
      Map<String, String> alias = new HashMap<String, String>();
      alias.put("tag",JpushUtils.NOTICE_TAG);
      alias.put("mobile",mobile);
      alias.put("title",JpushUtils.NOTICE_TITLE);
      alias.put("msg",JpushUtils.formatMsgStr(JpushUtils.NOTICE_MSG,new String[]{orderNo}));
      alias.put("param", JSONObject.toJSONString(params));
      PushPayload payload = buildPushObject_all_alias_alert(alias);
      try {
         PushResult result = jpushClient.sendPush(payload);
         LOG.info("Get result - " + result);
      } catch (APIConnectionException e) {
         e.printStackTrace();
         LOG.error("Connection error. Should retry later. ", e);
         LOG.error("Sendno: " + payload.getSendno());
      } catch (APIRequestException e) {
         e.printStackTrace();
         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());
      }
   }

   public static PushPayload buildPushObject_all_alias_alert(Map<String, String> parm) {
      String sound = "default";
      if(!StringUtils.isEmpty(parm.get("tag"))){
         sound = "jpush_prompt_tone" + parm.get("tag") + ".mp3";
      }
      return PushPayload.newBuilder()
              //所有平台的用户
              .setPlatform(Platform.all())
              .setAudience(Audience.alias(parm.get("mobile")))
              .setNotification(Notification.newBuilder()
                      //发送ios
                      .addPlatformNotification(IosNotification.newBuilder()
                              //消息体
                              .setAlert(parm.get("msg"))
                              .setBadge(+1)
                              //ios提示音
                              .setSound(sound)
                              //附加参数
                              .addExtras(parm)
                              .build())
                      //发送android
                      .addPlatformNotification(AndroidNotification.newBuilder()
                              //消息体
                              .setAlert(parm.get("msg"))
                              .setTitle(parm.get("title"))
                              .addExtras(parm)
                              .build())
                      .build())
              .build();
   }

   /**
    * @Description:格式化消息体
    * @Author: duyaqiong
    * @Date: 2020/9/15 9:43
    * @param msg
    * @param params
    * @Return: java.lang.String
    **/
   public static String formatMsgStr(String msg,String[] params){
      return  MessageFormat.format(msg, params);
   }
}

3.使用

JpushUtils.sendJGNotice(mobile,orderNo);

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值