个推推送项目实用(二)

第二步:直接上代码,不废话,有问题联系我留言
这里写图片描述
声明:个推里面的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
    评论
这个资源是一个基于Spring Boot的智能热度分析和自媒体推送平台项目,它旨在帮助自媒体人员分析内容的热度,并根据分析结果进行有针对性的内容推送。以下是对该项目的详细介绍: 1. **技术栈与框架**: - 后端使用Java语言开发,利用Spring Boot框架简化配置和部署流程。 - 前端可能采用了Vue.js或其他主流的JavaScript框架,以构建用户友好的交互界面(具体技术未提及)。 - 数据库选用MySQL,用于存储用户信息、内容数据及热度分析结果。 - 系统可能还集成了数据分析工具或库,用于实现智能热度分析功能。 2. **核心功能与模块**: - 提供用户注册、登录认证机制,确保平台的安全性。 - 实现自媒体内容的智能热度分析,通过算法计算内容的热门程度。 - 根据热度分析结果,自动推送相关内容给目标用户群体。 - 管理后台,供运营人员管理用户、内容和推送策略。 - 可视化的数据报告,使用户能够直观了解内容的表现。 3. **系统设计优势**: - 高效的数据处理能力,能够快速响应用户的请求。 - 良好的扩展性,方便未来添加新的功能或进行升级。 - 用户界面友好,便于用户理解和操作。 - 强大的数据分析功能,助力自媒体人员更好地把握市场动态。 4. **开发环境说明**: - 荐使用的JDK版本为1.8以上,以保证兼容性和稳定性。 - 内置Tomcat作为Web服务器,简化了应用的部署过程。 - 使用Maven或Gradle作为项目构建工具,便于依赖管理和项目构建。 综上所述,这个智能热度分析和自媒体推送平台项目提供了一个全面的解决方案,适用于希望提升内容广效果的自媒体运营者。它不仅包括了实用的功能,还注重了用户体验和系统的可维护性。
为了满足广大Android开发爱好者与从业者的学习需求,我们精心整理并上传了一份全面而实用的Android项目资源包。这份资源包内容丰富,涵盖了从基础知识到实战应用的全方位内容,旨在为开发者们提供一个便捷、高效的学习平台。 一、文件手册 资源包中的文件手册部分,详细记录了Android开发的核心知识点和常用技术。无论是初学者还是有一定经验的开发者,都能从中找到所需的学习资料。手册采用了简洁明了的排版方式,使得查阅更加方便快捷。同时,手册内容深入浅出,既适合新手入门,也能为老手提供有价值的参考。 项目实战与练习 为了让学习者能够将理论知识与实践相结合,我们特别准备了项目实战与练习部分。这部分内容包含了多个精心设计的Android项目案例,从需求分析、设计思路到实现过程,都有详细的讲解和代码示例。学习者可以通过实际操作,深入了解Android开发的整个流程,提升自己的实战能力。 此外,我们还提供了一系列练习题,旨在巩固所学知识,检验学习成果。这些练习题既有基础题,也有难度较高的挑战题,适合不同层次的学习者进行练习。 三、Android开发工具集 在Android开发过程中,选择合适的工具能够大大提高开发效率。因此,我们整理了常用的Android开发工具集,包括开发工具、测试工具、性能优化工具等。这些工具都是经过我们精心筛选和测试的,能够帮助开发者们更加高效地进行Android开发工作。 总的来说,这份Android项目资源包是一份不可多得的学习资料,无论你是初学者还是有一定经验的开发者,都能从中受益匪浅。我们希望通过这份资源包,为广大Android开发爱好者与从业者提供一个更加便捷、高效的学习平台,共同动Android开发领域的发展。
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

战神丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值