当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot +Dcloud个推成功案例 复制即用

SpringBoot +Dcloud个推成功案例 复制即用

2020年07月24日  | 移动技术网IT编程  | 我要评论


**此篇借鉴了原文HEY ON博客的push案例,做了修改,因为在实际场景种透传消息是应用最广的(基本上只用透传),所以主要修改了方法参数和透传模板的数据写入传递等……
本项目因为是单体服务应用对两个App进行推送服务,所以改成了支持多应用的方式

依赖由于位置原因无法通过maven的方式添加依赖
使用的是本地jar包引用同时在打包jar的时候也会添加进
在这里插入图片描述**

<dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-fastjson</artifactId>
            <version>1.0.0.6</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/gexin-rp-fastjson-1.0.0.6.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-base</artifactId>
            <version>4.0.0.35</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/gexin-rp-sdk-base-4.0.0.35.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-http</artifactId>
            <version>4.1.1.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/gexin-rp-sdk-http-4.1.1.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-template</artifactId>
            <version>4.0.0.27</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/gexin-rp-sdk-template-4.0.0.27.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.5.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/protobuf-java-2.5.0.jar</systemPath>
        </dependency>

调用方法赋值的实体

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class PushMessage {

    /**
     * 1-打开应用首页
     * 2-打开应用内页面
     * 3-点击通知打开网页模板
     * 4-透传消息模版
     * 5-iOS模版说明
     * 6-通知消息撤回
     * 7-停止批量推
     */
    private Integer model;

    //目标cid,群推,逗号隔开
    private String cid;

    //抬头
    private String title;

    //内容
    private String content;

    //图标链接,写了安卓能看见,ios看不见,可以不写
    private String iconUrl;

    //自定义消息id,可以用以后继覆盖
    private Integer msgId;

    //透传时写json体,普通模板不需要
    private String intent;

    //跳转网页链接,基本不用写
    private String goUrl;

    //app频道 1/2
    private String appChannel;

单推方法的实例部分

其实透传模板是可以不传IconUrl(就是手机通知栏会显示的小图标)和MsgId(对推送后续撤销,覆盖之类的操作,我自己的项目中暂时用不到这些功能)

JSONObject json = new JSONObject();
json.put("title", "支付成功");
json.put("content", "歪比巴卜");
json.put("jumpPath", "/pages/tabBar/mines/index");
json.put("iconUrl", qiniupath + "upload/asdwaadgage.jpg" + imgSuffix);
	pushUtil.singlePush(new PushMessage()
		.setModel(AppPushUtil.penetrateModel)
        .setCid(userMapper.getOrderCid(out_trade_no))
        .setTitle(json.get("title").toString())
        .setIntent(json.toString())
        .setContent(json.get("content").toString())
        .setIconUrl(json.get("iconUrl").toString())
        .setMsgId(Integer.valueOf(Base64.random(3, true)))
        .setAppChannel(AppPushUtil.potFriends));

群推的方法实例

	public void listPushToXXX() {
		List<String> cids = userService.getXXX();
        JSONObject json=new JSONObject();
        json.put("title","歪比巴卜");
        json.put("content","阿巴阿巴阿巴");
        json.put("iconUrl",qiniupath + "upload/asd13w1a65d1s32a1d.png" + imgSuffix);
        //如果超过800人就切割,保险起见因为个推一次批量推1000个
        if (cids.size() > 800) {
        	//分割集合
            List<List<String>> cidss = splitList(cids, 800);
            for (int i = 0; i < cidss.size(); i++) {
                pushUtil.listPush(new PushMessage()
                        .setCid(cidss.get(i).toString().substring(1, cids.toString().length() - 1).replaceAll(" ", "")) //集合转数组的字符长串,再逗号间隔
                        .setModel(AppPushUtil.penetrateModel)
                        .setAppChannel(AppPushUtil.artists)
                        .setIntent(json.toString())
                        .setTitle(json.get("title").toString())
                        .setContent(json.get("content").toString())
                        .setIconUrl(json.get("iconUrl").toString())
                        .setMsgId(Integer.valueOf(Base64.random(3, true))));
                personalNoticeMapper.insertPersonalNoticeUserList(NoticeTemplateEnum.SOMEONE_INVITATION_APPRAISAL.getTemplateTilte(), cidss.get(i));
            }
        } else {
            pushUtil.listPush(new PushMessage()
                    .setCid(cids.toString().substring(1, cids.toString().length() - 1).replaceAll(" ", ""))
                    .setModel(AppPushUtil.penetrateModel)
                    .setAppChannel(AppPushUtil.artists)
                    .setIntent(json.toString())
                    .setTitle(json.get("title").toString())
                    .setContent(json.get("content").toString())
                    .setIconUrl(json.get("iconUrl").toString())
                    .setMsgId(Integer.valueOf(Base64.random(3, true))));
        }
    }


    private List<List<String>> splitList(List<String> cids, int groupSize) {
        int length = cids.size();
        // 计算可以分成多少组
        int num = (length + groupSize - 1) / groupSize; // TODO
        List<List<String>> newList = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            // 开始位置
            int fromIndex = i * groupSize;
            // 结束位置
            int toIndex = (i + 1) * groupSize < length ? (i + 1) * groupSize : length;
            newList.add(cids.subList(fromIndex, toIndex));
        }
        return newList;
    }

下面是工具方法类

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IIGtPush;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.IQueryResult;
import com.gexin.rp.sdk.base.impl.*;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.dto.GtReq;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.*;
import com.gexin.rp.sdk.template.style.Style0;
import com.gexin.rp.sdk.template.style.Style6;
import com.sun.org.apache.bcel.internal.generic.PUSH;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class AppPushUtil {
	//双app
    public static final String app1= "1";
    public static final String app2= "2";
    //模板代号
    public static final Integer appIndexModel = 1;
    public static final Integer appPageModel = 2;
    public static final Integer htmlModel = 3;
    public static final Integer penetrateModel = 4;
    public static final Integer iosModel = 5;
    public static final Integer notifyMsgRollBack = 6;
    public static final Integer StopPushModel = 7;

    private static final String ONLINE = "Online";
    private static final String OFFLINE = "Offline";
	
	//厂商通道,后面还有参数在模板种再具体拼接
    private static String app1Intent = "intent:" +
            "#Intent;package=com.xx.xxx;" +
            "launchFlags=0x14000000;" +
            "component=com.xx.xxx/io.dcloud.PandoraEntry;" +
            "S.UP-OL-SU=true;";

    private static String app2Intent = "intent:" +
            "#Intent;package=com.yy.yyy;" +
            "launchFlags=0x14000000;" +
            "component=com.yy.yyy/io.dcloud.PandoraEntry;" +
            "S.UP-OL-SU=true;";

    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

    private static String app1Id = "xxxxxxxxxxxxxxx";

    private static String app2Id = "xxxxxxxxxxxxxxx";

    private static String app1Key = "yyyyyyyyyyyyyyyyyyyyyyyyyy";

    private static String app2Key = "yyyyyyyyyyyyyyyyyyyyyyyyyy";

    private static String app1MasterSecret = "zzzzzzzzzzzzzzzzzzz";

    private static String app2MasterSecret = "zzzzzzzzzzzzzzzzzzz";

    //app2端推送
    private static IGtPush app2Push = null;

    //app1端推送
    private static IGtPush app1Push = null;

    /**
     * postConstruct注解使被注解的方法在程序启动时就执行它,以此来初始化
     */
    @PostConstruct
    private void initPush() {
        app2Push = new IGtPush(app2Key , app2MasterSecret );
        app1Push = new IGtPush(app1Key , app1MasterSecret );
    }

    //获取上面初始化的推送工具
    protected IGtPush getPush(String appChannel) {
        if (app1.equalsIgnoreCase(appChannel)) { //app1
            return potFriendsPush;
        } else if (app2.equals(appChannel)) { //app2
            return artistsPush;
        }
        return null;
    }

    //推送目标工具
    private Target target;

    //根据app频道获取目标工具
    protected Target getTarget(String appChannel) {
        if (app1.equalsIgnoreCase(appChannel)) { 
            target = new Target();
            target.setAppId(app1Id);
        } else if (app2.equalsIgnoreCase(appChannel)) { 
            target = new Target();
            target.setAppId(app2Id);
        }
        return target;
    }

    /**
     * 单个用户推送
     *
     * @param pushMessage
     * @return
     */
    public String singlePush(PushMessage pushMessage) {
        //获取推送工具
        IGtPush push = getPush(pushMessage.getAppChannel());
        SingleMessage message = new SingleMessage();
        setMessageData(message, pushMessage);
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(72 * 3600 * 1000);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        Target target = getTarget(pushMessage.getAppChannel());
        target.setClientId(pushMessage.getCid());
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            return ret.getResponse().toString();
        } else {
            log.error("singlePush个推服务器响应异常");
            return "";
        }
    }

    /**
     * 用户列表推送  单次推送数量限制1000以内,此接口频次限制200万次/天
     *
     * @param pushMessage
     */
    public String listPush(PushMessage pushMessage) {
        IIGtPush push = getPush(pushMessage.getAppChannel());
        ListMessage message = new ListMessage();
        setMessageData(message, pushMessage);
        // 设置消息离线,并设置离线时间
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(72 * 3600 * 1000);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        // 配置推送目标
        List targets = new ArrayList<>();
        String[] cids = pushMessage.getCid().split(",");
        if (cids.length > 0) {
            for (String cid : cids) {
                Target target = getTarget(pushMessage.getAppChannel());
                target.setClientId(cid);
                targets.add(target);
            }
            // taskId用于在推送时去查找对应的message
            String taskId = push.getContentId(message);
            IPushResult ret = null;
            try {
                ret = push.pushMessageToList(taskId, targets);
            } catch (RequestException e) {
                e.printStackTrace();
                ret = push.pushMessageToList(taskId, targets);
            }
            if (ret != null) {
                return ret.getResponse().toString();
            } else {
                log.error("listPush个推服务器响应异常");
                return "";
            }
        }
        return "";
    }

    /**
     * 全部用户推送
     *
     * @param pushMessage
     * @return
     */
    public String allPush(PushMessage pushMessage) {
        IGtPush push = getPush(pushMessage.getAppChannel());
        AppMessage message = new AppMessage();
        setMessageData(message, pushMessage);
        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(72 * 1000 * 3600);
        //推送给App的目标用户需要满足的条件
        AppConditions cdt = new AppConditions();
        List appIdList = new ArrayList();
        appIdList.add(appId);
        message.setAppIdList(appIdList);
        //手机类型
        List phoneTypeList = new ArrayList();
        //省份
        List provinceList = new ArrayList();
        //自定义tag
        List tagList = new ArrayList();
        cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
        cdt.addCondition(AppConditions.REGION, provinceList);
        cdt.addCondition(AppConditions.TAG, tagList);
        message.setConditions(cdt);
        IPushResult ret = push.pushMessageToApp(message, "任务别名_toApp");
        return ret.getResponse().toString();
    }

    void setTemplate(String appChannel, AbstractTemplate template) {
        if (potFriends.equals(appChannel)) {
            template.setAppId(appId);
            template.setAppkey(appKey);
        } else if (artists.equals(appChannel)) {
            template.setAppId(artistAppId);
            template.setAppkey(artistAppKey);
        }
    }


    /****************************************************************************************************************
     模板 1-打开应用首页 2-打开应用内页面 3-点击通知打开网页模板 4-透传消息模版 5-iOS模版说明 6-通知消息撤回 7-停止批量推
     *****************************************************************************************************************/
    //1 打开应用首页
    public NotificationTemplate notificationTemplateDemo(PushMessage pushMessage) {
        NotificationTemplate template = new NotificationTemplate();
        //template.setAppId(appId);
        //template.setAppkey(appKey);
        setTemplate(pushMessage.getAppChannel(), template);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(1);
        //透传内容,不在通知栏中展示,自定义内容,开发者自行处理,不支持转义字符
        //规定intent规则:zw://项目名/activity 全路径?parm1=value1&parm2=value2
        template.setTransmissionContent(pushMessage.getIntent());
        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(pushMessage.getTitle());
        style.setText(pushMessage.getContent());
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl(pushMessage.getIconUrl());
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        //style.setChannel("默认channel");
        //style.setChannelName("默认channel名称");
        style.setChannelLevel(3);//3:有声音,有震动,锁屏和通知栏中都显示,通知唤醒屏幕。(推荐)
        template.setStyle(style);
        // 设置定时展示时间,安卓机型可用
        // template.setDuration("2019-08-16 11:40:00", "2019-08-16 12:24:00");
        // 消息覆盖
        template.setNotifyid(pushMessage.getMsgId()); // 在消息推送的时候设置自定义的notifyid。如果需要覆盖此条消息,则下次使用相同的notifyid发一条新的消息。客户端sdk会根据notifyid进行覆盖。
        template.setAPNInfo(getAPNPayload(pushMessage));
        return template;
    }

    //2 打开应用内页面
    public StartActivityTemplate startActivityTemplateDemo(PushMessage pushMessage) {
        StartActivityTemplate template = new StartActivityTemplate();
        setTemplate(pushMessage.getAppChannel(), template);
        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(pushMessage.getTitle());
        style.setText(pushMessage.getContent());
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl(pushMessage.getIconUrl());
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        //style.setChannel("默认channel");
        //style.setChannelName("默认channel名称");
        style.setChannelLevel(3);
        template.setStyle(style);
        template.setAPNInfo(getAPNPayload(pushMessage));
        //intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end
        //String intent = "intent:#Intent;component=com.yourpackage/.NewsActivity;end";
        template.setIntent(pushMessage.getIntent()); //目标页面地址,最大长度限制为1000
        // 设置定时展示时间,安卓机型可用
        // template.setDuration("2019-08-16 11:40:00", "2019-08-16 12:24:00");
        // 消息覆盖
        template.setNotifyid(pushMessage.getMsgId()); // 在消息推送的时候设置自定义的notifyid。如果需要覆盖此条消息,则下次使用相同的notifyid发一条新的消息。客户端sdk会根据notifyid进行覆盖。
        return template;
    }

    //3 点击通知打开网页模板
    public LinkTemplate linkTemplateDemo(PushMessage pushMessage) {
        LinkTemplate template = new LinkTemplate();
        setTemplate(pushMessage.getAppChannel(), template);
        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(pushMessage.getTitle());
        style.setText(pushMessage.getContent());
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl(pushMessage.getIconUrl());
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        //style.setChannel("默认");
        //style.setChannelName("默认");
        style.setChannelLevel(3);
        template.setStyle(style);
        template.setAPNInfo(getAPNPayload(pushMessage));
        // 设置打开的网址地址
        template.setUrl(pushMessage.getGoUrl());
        /* 在消息推送的时候设置自定义的notifyid。
         如果需要覆盖此条消息,则下次使用相同的notifyid发一条新的消息。
         客户端sdk会根据notifyid进行覆盖。*/
        template.setNotifyid(pushMessage.getMsgId());
        return template;
    }

    //4 透传消息模版
    public TransmissionTemplate transmissionTemplateDemo(PushMessage pushMessage) {
        TransmissionTemplate template = new TransmissionTemplate();
        setTemplate(pushMessage.getAppChannel(), template);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(2);
        //cid的在线状态
        //boolean cidStatus = getCidStatus(pushMessage.getAppChannel(), pushMessage.getCid());
        //在线
        template.setTransmissionContent(pushMessage.getIntent());
        //不在线,走厂商通道
        Notify notify = new Notify();
        if (potFriends.equals(pushMessage.getAppChannel())) {
            notify.setIntent(potFriendsIntent +
                    "S.title=" + pushMessage.getTitle() +
                    ";S.content=" + pushMessage.getContent() + ";S.payload=" + pushMessage.getIntent() + ";end");
        } else if (artists.equals(pushMessage.getAppChannel())) {
            notify.setIntent(artistsIntent +
                    "S.title=" + pushMessage.getTitle() +
                    ";S.content=" + pushMessage.getContent() + ";S.payload=" + pushMessage.getIntent() + ";end");
        }
        notify.setPayload(pushMessage.getIntent());
        notify.setTitle(pushMessage.getTitle());
        notify.setContent(pushMessage.getContent());
        notify.setType(GtReq.NotifyInfo.Type._intent);
        template.set3rdNotifyInfo(notify);
        //ios
        template.setAPNInfo(getAPNPayload(pushMessage/*, cidStatus*/));
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
        return template;
    }

    //5 iOS模版说明(JAVA)
    public TransmissionTemplate getTemplate(PushMessage pushMessage) {
        TransmissionTemplate template = new TransmissionTemplate();
        setTemplate(pushMessage.getAppChannel(), template);
        template.setTransmissionContent(pushMessage.getIntent());//透传内容
        template.setTransmissionType(2);
        template.setAPNInfo(getAPNPayload(pushMessage));
        return template;
    }


    //6.通知消息撤回,通过自定义的msgId
    public RevokeTemplate revokeTemplateDemo(PushMessage pushMessage) {
        RevokeTemplate template = new RevokeTemplate();
        setTemplate(pushMessage.getAppChannel(), template);
        template.setOldTaskId(pushMessage.getMsgId().toString());
        template.setForce(false);
        return template;
    }

    //7.停止批量推
    public void stopListPushDemo(String contentId, String appChannel) {
        IIGtPush push = getPush(appChannel);
        push.stop(contentId);
    }

    private static APNPayload getAPNPayload(PushMessage pushMessage/*, Boolean cidStatus*/) {
        APNPayload payload = new APNPayload();
        //自定义消息json体
        payload.addCustomMsg("msg",pushMessage.getIntent());
        //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
        payload.setContentAvailable(0);//推送直接带有透传数据
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
        //payload.setCategory("$由客户端定义");
        //payload.addCustomMsg("由客户自定义消息key", "由客户自定义消息value");
        //简单模式APNPayload.SimpleMsg
        //payload.setAlertMsg(new APNPayload.SimpleAlertMsg(pushMessage.getIntent()));
        payload.setAlertMsg(getDictionaryAlertMsg(pushMessage));  //字典模式使用APNPayload.DictionaryAlertMsg
        /*if (!cidStatus) {
            //app不在前台运行,推送简要信息,由个推服务直接展示在通知栏
            JSONObject json = JSONObject.parseObject(pushMessage.getIntent());
            payload.setAlertMsg(new APNPayload.SimpleAlertMsg(json.get("title") + "\n\t" + json.get("content")));
        } else {
            //app在前台,推送完整json格式信息,交由客户端自己处理
            payload.setAlertMsg(new APNPayload.SimpleAlertMsg(pushMessage.getIntent()));
        }*/
        return payload;
    }

    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(
            PushMessage pushMessage) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody(pushMessage.getContent());
        alertMsg.setTitle(pushMessage.getTitle());
        alertMsg.setLaunchImage(pushMessage.getIconUrl());
       /* alertMsg.setActionLocKey("ActionLockey");
        alertMsg.setLocKey("loc-key1");
        alertMsg.addLocArg("loc-ary1");
        alertMsg.setLaunchImage("launch-image");*/
        // IOS8.2以上版本支持

        /*alertMsg.setTitleLocKey("自定义通知标题");
        alertMsg.addTitleLocArg("自定义通知标题组");*/
        return alertMsg;
    }

    /**
     * 判断用户当前是否在线
     *
     * @param appChannel
     * @param cid
     * @return
     */
    public boolean getCidStatus(String appChannel, String cid) {
        IQueryResult clientIdStatus = null;
        if (potFriends.equals(appChannel)) {
            clientIdStatus = potFriendsPush.getClientIdStatus(appId, cid);
        } else if (artists.equals(appChannel)) {
            clientIdStatus = artistsPush.getClientIdStatus(artistAppId, cid);
        }
        Map<String, Object> response = clientIdStatus.getResponse();
        if (ONLINE.equals(response.get("result").toString())) {
            return true;
        } else if (OFFLINE.equals(response.get("result").toString())) {
            return false;
        }
        return false;
    }


    /**
     * 设置消息数据
     *
     * @param message
     * @param pushMessage
     */
    private void setMessageData(Message message, PushMessage pushMessage) {
        int model = pushMessage.getModel();
        switch (model) {
            case 1: {
                //app首页
                NotificationTemplate template = notificationTemplateDemo(pushMessage);
                message.setData(template);
            }
            break;
            case 2: {
                //app内页
                StartActivityTemplate template = startActivityTemplateDemo(pushMessage);
                message.setData(template);
            }
            break;
            case 3: {
                //网页
                LinkTemplate template = linkTemplateDemo(pushMessage);
                message.setData(template);
            }
            break;
            case 4: {
                //透传消息模版
                TransmissionTemplate template = transmissionTemplateDemo(pushMessage);
                message.setData(template);
                message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
            }
            break;
            case 5: {
                //iOS模版说明(JAVA)
                TransmissionTemplate template = getTemplate(pushMessage);
                message.setData(template);
            }
            break;
            case 6: {
                //通知消息撤回
                RevokeTemplate template = revokeTemplateDemo(pushMessage);
                message.setData(template);
            }
            break;
            default: {
                //点击通知打开应用模板
                NotificationTemplate template = notificationTemplateDemo(pushMessage);
                message.setData(template);
            }
            break;
        }
//        return message;
    }


  /*  public static void main(String[] args) {
        PushMessage pushMessage = new PushMessage();
        pushMessage.setModel(8);//模式,详看代码
        //待推送的用户ID
        pushMessage.setSevCId("a6ef8ba4bb0ccd91205ac25c1967f4f6");
        pushMessage.setTitle("测试标题");
        pushMessage.setContent("测试内容");
        pushMessage.setIconUrl("测试网络icon");
        pushMessage.setMsgId(123);//可用户覆盖的消息id
        pushMessage.setIntent("透传内容");
        String strResponse = new AppPushUtil().singlePush(pushMessage);
        System.out.println("返回内容:" + strResponse);
    }*/

}

至此基本就稳推了,剩下的交给厂商通道和前端自己处理透传消息内容了,我这儿前端是uni-app,怎么做我就不提供了

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网