个推推送项目实用(二)

第二步:直接上代码,不废话,有问题联系我留言
这里写图片描述
声明:个推里面的demo代码应用到实际项目中,需要改编成工具类!!!
需要配置的Maven包

<!--消息推送-->
 <dependency>
      <groupId>com.gexin.platform</groupId>
      <artifactId>gexin-rp-sdk-http</artifactId>
      <version>4.0.1.7</version>
    </dependency>
    <dependency>
      <groupId>com.gexin.platform</groupId>
      <artifactId>gexin-rp-sdk-template</artifactId>
      <version>4.0.0.6</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.gexin.platform</groupId>
      <artifactId>gexin-rp-sdk-template</artifactId>
      <version>4.0.0.6</version>
    </dependency>

工具类代码:

package cn.xdf.api.business.app.notice.common;

import cn.xdf.api.business.manager.common.entity.AppMsgDevice;
import cn.xdf.api.business.manager.common.entity.AppMsgInfo;
import cn.xdf.api.common.BusinessConstant;
import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息推送工具类
 */
public class NoticeUtil {

    private static Logger log = LoggerFactory.getLogger(NoticeUtil.class);

    private static final String IOS = "iOS";
    private static final String ANDROID = "Android";

    public static void main(String[] args) throws Exception {

    }

    /**
     * 发送消息至所有安装app用户
     * 通过clientid 发送
     */
    public static IPushResult pushMsgToAll(AppMsgInfo appMsgInfo){
        if(StringUtils.isEmpty(appMsgInfo)){
            log.error("in NoticeUtil#pushMsgToUsers method,appMsgInfo is null");
            return null;
        }
        System.setProperty("gexin_pushList_needDetails", "true");
        IGtPush push = new IGtPush(BusinessConstant.geTuiHost, BusinessConstant.appKey, BusinessConstant.masterSecret);
        AppMessage message = new AppMessage();
        // 通知透传模板
        TransmissionTemplate template = getTransmissionTemplate(appMsgInfo);
        message.setData(template);
        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        //推送给App的目标用户需要满足的条件
        AppConditions cdt = new AppConditions();

        List<String> appIdList = new ArrayList<String>();
        appIdList.add(BusinessConstant.appId);
        message.setAppIdList(appIdList);
        message.setConditions(cdt);
        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
        return ret;
    }

    /**
     * 分平台发送消息至app用户 Android、iOS
     * 通过clientid 发送
     */
    public static IPushResult pushMsgByType(AppMsgInfo appMsgInfo,String type){
        if(StringUtils.isEmpty(appMsgInfo) || !StringUtils.hasText(type)){
            log.error("in NoticeUtil#pushMsgToUsers method,appMsgInfo or type is null");
            return null;
        }
        System.setProperty("gexin_pushList_needDetails", "true");
        IGtPush push = new IGtPush(BusinessConstant.geTuiHost, BusinessConstant.appKey, BusinessConstant.masterSecret);
        AppMessage message = new AppMessage();

        TransmissionTemplate template = getTransmissionTemplate(appMsgInfo);
        message.setData(template);
        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        //推送给App的目标用户需要满足的条件
        AppConditions cdt = new AppConditions();
        List<String> appIdList = new ArrayList<String>();
        appIdList.add(BusinessConstant.appId);
        message.setAppIdList(appIdList);
        //手机类型
        List<String> phoneTypeList = new ArrayList<String>();
        phoneTypeList.add(type.toUpperCase());// Android/iOS
        cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
        message.setConditions(cdt);
        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
        return ret;
    }

    /**
     * 发送消息至指定app用户
     * 通过clientid 发送
     */
    public static IPushResult pushMsgToUsers(AppMsgInfo appMsgInfo, List<AppMsgDevice> appMsgDeviceList){
        if(appMsgDeviceList==null || appMsgDeviceList.size()==0 || StringUtils.isEmpty(appMsgInfo)){
            log.error("in NoticeUtil#pushMsgToUsers method,appMsgInfo or clientList is null");
            return null;
        }
        System.setProperty("gexin_pushList_needDetails", "true");
        List<Target> list = getTargets(appMsgDeviceList);
        // 配置返回每个别名及其对应cid的用户状态,可选
        IGtPush push = new IGtPush(BusinessConstant.geTuiHost, BusinessConstant.appKey, BusinessConstant.masterSecret);
        ListMessage message = new ListMessage();
        TransmissionTemplate template = getTransmissionTemplate(appMsgInfo);
        message.setData(template);
        // 设置消息离线,并设置离线时间
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 配置推送目标
        String taskId = push.getContentId(message);
        IPushResult ret = push.pushMessageToList(taskId, list);
        System.out.println(ret.getResponse().toString());
        return ret;
    }

    public static LinkTemplate getLinkTemplate(AppMsgInfo appMsgInfo){
        LinkTemplate template = new LinkTemplate();
        template.setAppId(BusinessConstant.appId);
        template.setAppkey(BusinessConstant.appKey);
        // 设置通知栏标题与内容
        template.setTitle(appMsgInfo.getTitle());//推送标题
        template.setText(appMsgInfo.getContent());//推送内容
        // 配置通知栏图标
        template.setLogo("");
        // 配置通知栏网络图标
        template.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        template.setUrl(appMsgInfo.getLink());//推送url路径
        return template;
    }

    public static TransmissionTemplate getTransmissionTemplate(AppMsgInfo appMsgInfo){
        TransmissionTemplate  template = new TransmissionTemplate ();
        template.setAppId(BusinessConstant.appId);
        template.setAppkey(BusinessConstant.appKey);
        template.setTransmissionContent(JSONObject.toJSON(appMsgInfo).toString());
        template.setTransmissionType(2);
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(1);
        payload.setSound("default");
        payload.setCategory("$由客户端定义");

        //简单模式APNPayload.SimpleMsg
        //  payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
        //自定义类型参数
        payload.addCustomMsg("type", appMsgInfo.getSendType());//推送类型1、全部,2android,3iOS,4用户Id
        payload.addCustomMsg("link", appMsgInfo.getLink());//推送url路径

        //字典模式使用APNPayload.DictionaryAlertMsg
        payload.setAlertMsg(getDictionaryAlertMsg(appMsgInfo));

        // 添加多媒体资源
   /* payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
            .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
            .setOnlyWifi(true));*/

        template.setAPNInfo(payload);
        return template;
    }

    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(AppMsgInfo appMsgInfo){
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setTitle(appMsgInfo.getTitle());//推送标题
        alertMsg.setBody(appMsgInfo.getContent());//推送内容
        alertMsg.setActionLocKey("ActionLockey");
        alertMsg.setLocKey("LocKey");
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image"); // iOS8.2以上版本支持 alertMsg.setTitle("Title");
        alertMsg.setTitleLocKey("TitleLocKey");
        alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }

    public static List<Target> getTargets(List<AppMsgDevice> appMsgDeviceList){
        List list = new ArrayList();
        for(int i=0;i<appMsgDeviceList.size();i++){
            Target temp = new Target();
            temp.setAppId(BusinessConstant.appId);
            temp.setClientId(appMsgDeviceList.get(i).getClientId());
            list.add(temp);
        }
        return list;
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

战神丶

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值