java个推工具(全部,指定,指定代参),附带前端代码+流程

在uniapp官网

这里是下面代码用到的参数
在这里插入图片描述

应用签名

需要前段给一个
在这里插入图片描述
在路径 直接输入 cmd
在这里插入图片描述

keytool -list -v -ketstore 5eeed265421a8.keystore

就能取到了

这里全部推送,和指定用户推送我给分开了,你们可以自己合成一个

全部推送

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.AbstractTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.StartActivityTemplate;
import com.gexin.rp.sdk.template.style.AbstractNotifyStyle;
import com.gexin.rp.sdk.template.style.Style0;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zyh
 * @Description: 描述
 * @CreateDate: 2020/5/30
 * @UpdateDate: 2020/5/30
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class GeTui {
    /**
     * 应用基本信息
     */
    private static final String APP_ID = "**********************";
    private static final String APP_KEY = "********************";
    private static final String APP_SECRET = "********************";
    private static final String MASTER_SECRET = "********************";

    /**
     * 样式模板Style0
     *
     * @param title   推送标题
     * @param text    推送内容
     * @param ring    收到通知是否响铃
     * @param vibrate 收到通知是否振动
     * @return
     */
    private static Style0 style0Base(String title, String text, boolean ring, boolean vibrate) {
        Style0 style = new Style0();
        // 设置推送标题
        style.setTitle(title);

        // 推送内容
        style.setText(text);

        // 设置响铃
        style.setRing(ring);

        // 设置震动
        style.setVibrate(vibrate);
        return style;
    }


    /**
     * 通知模板
     *
     * @param page
     * @param style
     * @return
     */
    public static StartActivityTemplate startActivityTemplateDemo(String page, AbstractNotifyStyle style) {
        StartActivityTemplate template = new StartActivityTemplate();
        template.setAppId(APP_ID);
        template.setAppkey(APP_KEY);


        template.setStyle(style);

        // 最大长度限制为1000
        String intent = "intent:#Intent;component=com.letui.app/pages/indexx/indexx;end";
        template.setIntent(intent);
        // Android 在消息推送的时候设置notifyid。如果需要覆盖此条消息,则下次使用
        // template.setNotifyid(123);
        // 设置定时展示时间,安卓机型可用
        // template.setDuration("2019-08-16 11:40:00", "2019-08-16 12:24:00");
        // 详见本页iOS通知样式设置
        // template.setAPNInfo(getAPNPayload());
        return template;
    }


    public static IPushResult push(String title, String text) {
        IGtPush push = new IGtPush(APP_KEY, MASTER_SECRET);

        AbstractNotifyStyle style = style0Base(title, text, true, true);
        AbstractTemplate template = startActivityTemplateDemo("", style);

        // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        List<String> appIds = new ArrayList<String>();
        appIds.add(APP_ID);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        // 设置离线推送
        message.setOffline(true);
        // 时间单位为毫秒
        message.setOfflineExpireTime(1000 * 600);

        // STEP6:执行推送
        IPushResult ret = push.pushMessageToApp(message);
        return ret;
    }

    public static void main(String[] args) {

        IGtPush push = new IGtPush(APP_KEY, MASTER_SECRET);

        Style0 style = new Style0();
        // STEP2:设置推送标题、推送内容
        style.setTitle("请输入通知栏标题");
        style.setText("请输入通知栏内容");
        // 设置推送图标
//        style.setLogo("push.png");

        // STEP3:设置响铃、震动等推送效果
        // 设置响铃
        style.setRing(true);
        // 设置震动
        style.setVibrate(true);


        // STEP4:选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(APP_ID);
        template.setAppkey(APP_KEY);
        template.setStyle(style);


        // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        List<String> appIds = new ArrayList<String>();
        appIds.add(APP_ID);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);  // 时间单位为毫秒

        // STEP6:执行推送
        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
    }

    public void xx(String title,String text){
        IGtPush push = new IGtPush(APP_KEY, MASTER_SECRET);

        Style0 style = new Style0();
        // STEP2:设置推送标题、推送内容
        style.setTitle(title);//标题
        style.setText(text);//内容
        // 设置推送图标
//        style.setLogo("push.png");

        // STEP3:设置响铃、震动等推送效果
        // 设置响铃
        style.setRing(true);
        // 设置震动
        style.setVibrate(true);


        // STEP4:选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(APP_ID);
        template.setAppkey(APP_KEY);
        template.setStyle(style);


        // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        List<String> appIds = new ArrayList<String>();
        appIds.add(APP_ID);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);  // 时间单位为毫秒

        // STEP6:执行推送
        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
    }



}

指定用户推送

我把大部分都注释了,可以选择的使用

指定推送需要 用户的cid,需要和前端传给后端

前端代码

在这里插入图片描述
在这里插入图片描述

//#ifdef APP-PLUS
   var info = plus.push.getClientInfo();
   uni.setStorageSync("clientid", info.clientid)
   console.log(uni.getStorageSync("clientid"))
   console.log(JSON.stringify(info), info.clientid);
   /* 5+  push 消息推送 ps:使用:H5+的方式监听,实现推送*/
   plus.push.addEventListener("click", function(msg) {
    console.log(msg)
    console.log("click:" + JSON.stringify(msg));
    console.log(msg.payload);
    console.log(msg.title);
    console.log(msg.content);
    uni.showModal({
     title: msg.title,
     content: msg.content,
     success: function(res) {
      if (res.confirm) {
       console.log('用户点击确定');
      } else if (res.cancel) {
       console.log('用户点击取消');
      }
     }
    });
   }, false);
   // 监听在线消息事件    
   plus.push.addEventListener("receive", function(msg) {
    console.log("recevice:" + JSON.stringify(msg))
    uni.showModal({
     title: msg.title,
     content: msg.content,
     success: function(res) {
      if (res.confirm) {
       console.log('用户点击确定');
      } else if (res.cancel) {
       console.log('用户点击取消');
      }
     }
    });
   }, false);
   //#endif
package back.minsu.utils.getui;/**
 * @Author: zyh
 * @Description: 描述
 * @CreateDate: 2020/7/6
 * @UpdateDate: 2020/7/6
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */

/**
 * @author: 云
 * @date: 2020/7/6 19:11
 * @version: 1.0
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IIGtPush;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;

public class GetuiUtils {

    private static String APP_ID = "*********";
    private static String APP_KEY = "******";
    private static String MASTER_SECRET = "******";
    //public static String host = "https://sdk.open.api.igexin.com/apiex.htm";
    public static String CID_A = "af43b23c6cb2ce40f8aeeb2e95a02fef";//在打包后的APP的js中获取 var cId = plus.push.getClientInfo().clientid;
    //public static String CID_B = "bae837b470994d614f0773097b92dbf3";
    public static IGtPush push;

    static {
        push = new IGtPush( APP_KEY, MASTER_SECRET);
    }

    /**
     * 绑定用户cid 别名
     *
     * @param //Alias
     * @param CID
     * @return
     */
//    public static boolean bindAlias(String alias, String CID) {
//        IAliasResult bindSCid = push.bindAlias(APP_ID, alias, CID);
//        if (bindSCid.getResult()) {
//            return true;
//        }
//        return false;
//    }

    /**
     * 对单个用户推送消息
     *
     * @param //alias
     * @param //msg
     * @return
     */
//    public static boolean pushMessageToSingle(String cid, String text, String transMsg) {
//        IGtPush push = new IGtPush(host, APP_KEY, MASTER_SECRET);
//        NotificationTemplate template = notificationTemplate("title", text, transMsg);
//        SingleMessage message = new SingleMessage();
//        message.setOffline(true);
//        // 离线有效时间,单位为毫秒,可选
//        message.setOfflineExpireTime(24 * 3600 * 1000);
//        message.setData(template);
//        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
//        message.setPushNetWorkType(0);
//        Target target = new Target();
//        target.setAppId(APP_ID);
//        target.setClientId(cid);
//        //target.setAlias(Alias);
//        IPushResult ret = null;
//        try {
//            ret = push.pushMessageToSingle(message, target);
//        } catch (RequestException e) {
//            e.printStackTrace();
//            ret = push.pushMessageToSingle(message, target, e.getRequestId());
//        }
//        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
//            System.out.println(ret.getResponse().toString());
//            if (ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")) {
//                return true;
//            }
//        }
//        return false;
//    }

    /**
     * 指定应用的所有用户群发推送消息
     *
     * @param //msg
     */
//    public static boolean pushtoAPP(String text, String transMsg) {
//        IGtPush push = new IGtPush(host, APP_KEY, MASTER_SECRET);
//
//        NotificationTemplate template = notificationTemplate("title", text, transMsg);
//        AppMessage message = new AppMessage();
//        message.setData(template);
//
//        message.setOffline(true);
//        //离线有效时间,单位为毫秒,可选
//        message.setOfflineExpireTime(24 * 1000 * 3600);
//        //推送给App的目标用户需要满足的条件
//        AppConditions cdt = new AppConditions();
//        List<String> appIdList = new ArrayList<String>();
//        appIdList.add(APP_ID);
//        message.setAppIdList(appIdList);
//        //手机类型
//        List<String> phoneTypeList = new ArrayList<String>();
//        //省份
//        List<String> provinceList = new ArrayList<String>();
//        //自定义tag
//        List<String> tagList = new ArrayList<String>();
//
//        cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
//        cdt.addCondition(AppConditions.REGION, provinceList);
//        cdt.addCondition(AppConditions.TAG, tagList);
//        message.setConditions(cdt);
//
//        IPushResult ret = push.pushMessageToApp(message, "msg_toApp");
//
//        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
//            System.out.println(ret.getResponse().toString());
//            if (ret.getResponse().get("result").toString().equals("ok")) {
//                return true;
//            }
//        }
//        return false;
//    }

    /**
     * 对单个用户推送透传消息
     *
     * @param //alias
     * @param //title
     * @param //content
     * @return
     */
//    public static boolean pushTransMessageToSingle(String cid, String msg) {
//        TransmissionTemplate template = transTemplate(cid, msg);
//        SingleMessage message = new SingleMessage();
//        message.setOffline(true);
//        // 离线有效时间,单位为毫秒,可选
//        message.setOfflineExpireTime(24 * 3600 * 1000);
//        message.setData(template);
//        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
//        message.setPushNetWorkType(0);
//        Target target = new Target();
//        target.setAppId(APP_ID);
//        target.setClientId(cid);
//        IPushResult ret = null;
//        try {
//            ret = push.pushMessageToSingle(message, target);
//        } catch (RequestException e) {
//            e.printStackTrace();
//            ret = push.pushMessageToSingle(message, target, e.getRequestId());
//        }
//        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
//            System.out.println(ret.getResponse().toString());
//            if (ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")) {
//                return true;
//            }
//        }
//        return false;
//    }

//    public static void pushMessageToIBatch(List<String> alias, String msg) {
//        IBatch batch = push.getBatch();
//        IPushResult ret = null;
//        try {
//            for (int i = 0; i < alias.size(); i++) {
//                // 构建客户a的透传消息a
//                constructClientTransMsg(alias.get(i), msg, batch);
//            }
//            // 构建客户B的点击通知打开网页消息b
//            // constructClientLinkMsg(CID_B,"msgB",batch);
//            ret = batch.submit();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(ret.getResponse().toString());
//    }

//    private static NotificationTemplate notificationTemplate(String title, String text, String obj) {
//        NotificationTemplate template = new NotificationTemplate();
//        // 设置APPID与APPKEY
//        template.setAppId(APP_ID);
//        template.setAppkey(APP_KEY);
//        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
//        template.setTransmissionType(1);
//        template.setTransmissionContent(obj);
//        // 设置定时展示时间
//        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
//        Style0 style = new Style0();
//        // 设置通知栏标题与内容
//        style.setTitle(title);
//        style.setText(text);
//        // 配置通知栏图标
//        style.setLogo("XXX");
//        // 配置通知栏网络图标
//        //style.setLogoUrl("");
//        // 设置通知是否响铃,震动,或者可清除
//        style.setRing(true);
//        style.setVibrate(true);
//        style.setClearable(true);
//        template.setStyle(style);
//
//        return template;
//    }

//    private static TransmissionTemplate transTemplate(String cid, String msg) {
//        TransmissionTemplate template = new TransmissionTemplate();
//        // 设置APPID与APPKEY
//        template.setAppId(APP_ID);
//        template.setAppkey(APP_KEY);
//        template.setTransmissionContent(msg);
//        template.setTransmissionType(0); // 这个Type为int型,填写1则自动启动app
//
//        return template;
//    }
    //点击通知打开应用模板
    public static void constructClientTransMsg(String cid, String msg, IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();
        NotificationTemplate template = new NotificationTemplate();//自定义模板
        template.setAppId(APP_ID);
        template.setAppkey(APP_KEY);
        //template.setTransmissionContent(msg);//消息内容
        template.setTransmissionType(1); // 这个Type为int型,填写1则自动启动app
        Style0 style = new Style0();        // 设置通知栏标题与内容
        style.setTitle("新消息通知");
        style.setText(msg);        // 配置通知栏图标
        //style.setLogo("icon.png");        // 配置通知栏网络图标
        //style.setLogoUrl("");//网络图标地址
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(60 * 60 * 1000);

        // 设置推送目标,填入appid和clientId
        Target target = new Target();
        target.setAppId(APP_ID);
        target.setClientId(cid);
        batch.add(message, target);
    }

    //点击通知打开网页消息
//    public static void constructClientLinkMsg(String cid, String msg, IBatch batch) throws Exception {
//
//        SingleMessage message = new SingleMessage();
//        LinkTemplate template = new LinkTemplate();
//        template.setAppId(APP_ID);
//        template.setAppkey(APP_KEY);
//        template.setTitle("title");
//        template.setText(msg);
//        template.setLogo("push.png");
//        template.setLogoUrl("logoUrl");
//        template.setUrl("http://www.baidu.com");
//
//        message.setData(template);
//        message.setOffline(true);
//        message.setOfflineExpireTime(60 * 1000);
//
//        // 设置推送目标,填入appid和clientId
//        Target target = new Target();
//        target.setAppId(APP_ID);
//        target.setClientId(cid);
//        batch.add(message, target);
//    }

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

        IIGtPush push = new IGtPush( APP_KEY, MASTER_SECRET);
        IBatch batch = push.getBatch();

        try {
            //构建客户a的透传消息a
            GetuiUtils.constructClientTransMsg(CID_A, "大哥大额" +
                    "00" +
                    "", batch);
            //构建客户B的点击通知打开网页消息b
//            constructClientLinkMsg(CID_B, "msgB", batch);
        } catch (Exception e) {
            e.printStackTrace();
        }
        batch.submit();
    }

}

指定用户推送并传参


import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.Constants;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;


/**
 * @Author: zyh
 * @Description: 描述
 * @CreateDate: 2020/5/30
 * @UpdateDate: 2020/5/30
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class GeTui {
    /**
     * 应用基本信息
     */
    private static final String APP_ID = "*****";
    private static final String APP_KEY = "******";
    private static final String MASTER_SECRET = "*****";
    public static String CID_A = "********";//在打包后的APP的js中获取 var cId = plus.push.getClientInfo().clientid;

    public static void main(String[] args) throws Exception {
        // 设置后,根据别名推送,会返回每个cid的推送结果
        System.setProperty(Constants.GEXIN_PUSH_SINGLE_ALIAS_DETAIL, "true");
        IGtPush push = new IGtPush(APP_KEY, MASTER_SECRET);
        NotificationTemplate template = getNotificationTemplate();
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间,单位为毫秒
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        // 厂商通道下发策略
        message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
        Target target = new Target();
        target.setAppId(APP_ID);
        target.setClientId(CID_A);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
        } else {
            System.out.println("服务器响应异常");
        }
    }



    public static NotificationTemplate getNotificationTemplate() {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(APP_ID);
        template.setAppkey(APP_KEY);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("请输入通知栏标题");
        style.setText("请输入通知栏内容");
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        style.setChannel("通知渠道id");
        style.setChannelName("通知渠道名称");
        style.setChannelLevel(3); //设置通知渠道重要性
        template.setStyle(style);

        template.setTransmissionType(1);  // 透传消息接受方式设置,1:立即启动APP,2:客户端收到消息后需要自行处理
        template.setTransmissionContent("请输入您要透传的内容");

        //template.setAPNInfo(getAPNPayload()); //详见【推送模板说明】iOS通知样式设置
        return template;
    }


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Nexus的REST API来获取指定组件的全部版本信息。以下是Java代码示例: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class NexusAPI { public static void main(String[] args) { String baseUrl = "http://your-nexus-repository-url/service/rest/v1/search/assets?"; String group = "com.example"; // 指定组件的Group ID String name = "example-library"; // 指定组件的Artifact ID String urlStr = baseUrl + "group=" + group + "&name=" + name; try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; while ((output = br.readLine()) != null) { // 解析返回的JSON数据,获取版本信息 // 这里可以使用JSON库来解析JSON数据,比如Jackson或者Gson System.out.println(output); } conn.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述代码中,我们构造了一个Nexus的REST API请求URL,指定了组件的Group ID和Artifact ID,然后使用Java的HttpURLConnection发送请求并获取响应。最后,解析返回的JSON数据,获取该组件的全部版本信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值