270 lines
11 KiB
Plaintext
270 lines
11 KiB
Plaintext
|
|
package com.jysoft.unipush;
|
|||
|
|
|
|||
|
|
|
|||
|
|
import com.aostarit.mobile.client.message.api.client.MsgQueryClient;
|
|||
|
|
import com.aostarit.mobile.client.message.api.client.MsgRequestClient;
|
|||
|
|
import com.aostarit.mobile.client.message.api.client.MsgSendClient;
|
|||
|
|
import com.aostarit.mobile.client.message.api.config.WxMsgConfig;
|
|||
|
|
import com.aostarit.mobile.client.message.api.entity.SendMsgParams;
|
|||
|
|
import com.aostarit.mobile.client.message.api.entity.WxSendMsgResult;
|
|||
|
|
import com.gexin.fastjson.JSON;
|
|||
|
|
import com.gexin.fastjson.JSONArray;
|
|||
|
|
import com.gexin.rp.sdk.base.IAliasResult;
|
|||
|
|
import com.gexin.rp.sdk.base.IBatch;
|
|||
|
|
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.base.impl.Transparent.Notification;
|
|||
|
|
import com.gexin.rp.sdk.base.notify.Notify;
|
|||
|
|
import com.gexin.rp.sdk.base.payload.APNPayload;
|
|||
|
|
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.TransmissionTemplate;
|
|||
|
|
import com.jysoft.unipush.template.PushStyle;
|
|||
|
|
import com.jysoft.unipush.template.PushTemplate;
|
|||
|
|
import com.nationalelectirc.Constant.Constant;
|
|||
|
|
import com.nationalelectirc.utils.RestResult;
|
|||
|
|
import com.nationalelectric.greenH5.GreenBaseController;
|
|||
|
|
import com.nationalelectric.greenH5.po.GreenNotice;
|
|||
|
|
import com.sgcc.uap.mdd.model.utils.StringUtil;
|
|||
|
|
import com.sgcc.uap.persistence.IHibernateDao;
|
|||
|
|
|
|||
|
|
import java.io.IOException;
|
|||
|
|
import java.net.URLEncoder;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.Date;
|
|||
|
|
import java.util.HashMap;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.stereotype.Controller;
|
|||
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|||
|
|
|
|||
|
|
import static com.jysoft.unipush.constant.AppInfo.*;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 别名管理相关
|
|||
|
|
*
|
|||
|
|
* @author wangjianxing
|
|||
|
|
* @see
|
|||
|
|
* @since 2020-07-17
|
|||
|
|
*/
|
|||
|
|
public class AliasManage2{
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* clientid绑定alias
|
|||
|
|
*
|
|||
|
|
* @author wangjianxing
|
|||
|
|
* @see
|
|||
|
|
* @since 2020-07-17
|
|||
|
|
*/
|
|||
|
|
public static void bindAlias(String clientId,String alias){
|
|||
|
|
//根据alias获取历史绑定的clientId
|
|||
|
|
try{
|
|||
|
|
IAliasResult ret = push.bindAlias(APPID, alias, clientId);
|
|||
|
|
System.out.println("绑定结果:" + ret.getResponse());
|
|||
|
|
ret = push.queryClientId(APPID, alias);
|
|||
|
|
System.out.println("根据别名获取的CID:" + ret.getResponse().get("cidlist"));
|
|||
|
|
|
|||
|
|
}catch(Exception e){
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 批量单推
|
|||
|
|
* <p>
|
|||
|
|
* 当单推任务较多时,推荐使用该接口,可以减少与服务端的交互次数。
|
|||
|
|
*/
|
|||
|
|
public static void pushToSingleBatch(List<Map<String,Object>> userList,String title,String text,String url) {
|
|||
|
|
// IBatch batch = push.getBatch();
|
|||
|
|
//
|
|||
|
|
// IPushResult ret = null;
|
|||
|
|
// try {
|
|||
|
|
// //构建客户a的透传消息a
|
|||
|
|
// for(int i=0;i<userList.size();i++){
|
|||
|
|
// String userId = (String) userList.get(i).get("id");
|
|||
|
|
// JSONArray cidArr = (JSONArray) push.queryClientId(APPID, userId).getResponse().get("cidlist");
|
|||
|
|
// if(cidArr==null||cidArr.size()==0){
|
|||
|
|
// continue;
|
|||
|
|
// }
|
|||
|
|
// for(int j=0;j<cidArr.size();j++){
|
|||
|
|
// String cid = cidArr.getString(j);
|
|||
|
|
// constructClientLinkMsg(cid, batch,title,text,url);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// ret = batch.submit();
|
|||
|
|
// } catch (Exception e) {
|
|||
|
|
// e.printStackTrace();
|
|||
|
|
// try {
|
|||
|
|
// ret = batch.retry();
|
|||
|
|
// } catch (IOException ex) {
|
|||
|
|
// ex.printStackTrace();
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// if (ret != null) {
|
|||
|
|
// System.out.println(ret.getResponse().toString());
|
|||
|
|
// } else {
|
|||
|
|
// System.out.println("服务器响应异常");
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 单推
|
|||
|
|
* <p>
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
public static void pushToSingle(String userId,String title,String text,String url) {
|
|||
|
|
// IBatch batch = push.getBatch();
|
|||
|
|
//
|
|||
|
|
// IPushResult ret = null;
|
|||
|
|
// try {
|
|||
|
|
// JSONArray cidArr = (JSONArray) push.queryClientId(APPID, userId).getResponse().get("cidlist");
|
|||
|
|
// for(int j=0;j<cidArr.size();j++){
|
|||
|
|
// String cid = cidArr.getString(j);
|
|||
|
|
// constructClientLinkMsg(cid, batch,title,text,url);
|
|||
|
|
// }
|
|||
|
|
// ret = batch.submit();
|
|||
|
|
// } catch (Exception e) {
|
|||
|
|
// e.printStackTrace();
|
|||
|
|
// try {
|
|||
|
|
// ret = batch.retry();
|
|||
|
|
// } catch (IOException ex) {
|
|||
|
|
// ex.printStackTrace();
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// if (ret != null) {
|
|||
|
|
// System.out.println(ret.getResponse().toString());
|
|||
|
|
// } else {
|
|||
|
|
// System.out.println("服务器响应异常");
|
|||
|
|
// }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private static void constructClientTransMsg(String cid, IBatch batch) throws Exception {
|
|||
|
|
AbstractTemplate template = PushTemplate.getTransmissionTemplate();
|
|||
|
|
SingleMessage message = getSingleMessage(template);
|
|||
|
|
|
|||
|
|
// 设置推送目标,填入appid和clientId
|
|||
|
|
Target target = new Target();
|
|||
|
|
target.setAppId(APPID);
|
|||
|
|
target.setClientId(cid);
|
|||
|
|
batch.add(message, target);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void constructClientLinkMsg(String cid, IBatch batch,String title,String text,String url) throws Exception {
|
|||
|
|
// if(StringUtil.isEmpty(url)){
|
|||
|
|
// url = "io.dcloud.PandoraEntry";
|
|||
|
|
// }
|
|||
|
|
url=URLEncoder.encode(url);
|
|||
|
|
System.out.println(url);
|
|||
|
|
StartActivityTemplate template = new StartActivityTemplate();
|
|||
|
|
|
|||
|
|
//NotificationTemplate template = new NotificationTemplate();
|
|||
|
|
|
|||
|
|
//TransmissionTemplate template = new TransmissionTemplate();
|
|||
|
|
template.setAppId(APPID);
|
|||
|
|
template.setAppkey(APPKEY);
|
|||
|
|
template.setStyle(PushStyle.getStyle6(title,text));
|
|||
|
|
|
|||
|
|
String intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNIE2E9D29/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title+";S.content="+text+";S.payload="+url+";end";
|
|||
|
|
template.setIntent(intent); //最大长度限制为1000
|
|||
|
|
// APNPayload payload = new APNPayload();
|
|||
|
|
// payload.setAlertMsg(getDictionaryAlertMsg(title,text));
|
|||
|
|
// payload.addCustomMsg("payload",url);
|
|||
|
|
template.setAPNInfo(getAPNPayload(title,text,url));
|
|||
|
|
|
|||
|
|
SingleMessage message = getSingleMessage(template);
|
|||
|
|
|
|||
|
|
// 设置推送目标,填入appid和clientId
|
|||
|
|
Target target = new Target();
|
|||
|
|
target.setAppId(APPID);
|
|||
|
|
target.setClientId(cid);
|
|||
|
|
batch.add(message, target);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private static APNPayload getAPNPayload(String title,String text,String url) {
|
|||
|
|
APNPayload payload = new APNPayload();
|
|||
|
|
payload.addCustomMsg("payload",url);
|
|||
|
|
//payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
|
|||
|
|
payload.setAlertMsg(getDictionaryAlertMsg(title,text)); //字典模式使用APNPayload.DictionaryAlertMsg
|
|||
|
|
return payload;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title,String text) {
|
|||
|
|
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
|
|||
|
|
alertMsg.setBody(text);
|
|||
|
|
// alertMsg.setActionLocKey("显示关闭和查看两个按钮的消息");
|
|||
|
|
// alertMsg.setLocKey("loc-key1");
|
|||
|
|
// alertMsg.addLocArg("loc-ary1");
|
|||
|
|
// alertMsg.setLaunchImage("调用已经在应用程序中绑定的图形文件名");
|
|||
|
|
// iOS8.2以上版本支持
|
|||
|
|
alertMsg.setTitle(title);
|
|||
|
|
// alertMsg.setTitleLocKey("自定义通知标题");
|
|||
|
|
// alertMsg.addTitleLocArg("自定义通知标题组");
|
|||
|
|
return alertMsg;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static SingleMessage getSingleMessage(AbstractTemplate template) {
|
|||
|
|
SingleMessage message = new SingleMessage();
|
|||
|
|
message.setData(template);
|
|||
|
|
// 设置消息离线,并设置离线时间
|
|||
|
|
message.setOffline(true);
|
|||
|
|
// 离线有效时间,单位为毫秒,可选
|
|||
|
|
message.setOfflineExpireTime(72 * 3600 * 1000);
|
|||
|
|
// 判断客户端是否wifi环境下推送。1为仅在wifi环境下推送,0为不限制网络环境,默认不限
|
|||
|
|
message.setPushNetWorkType(0);
|
|||
|
|
//message.setPushNetWorkType(0); // 判断客户端是否wifi环境下推送。1为仅在wifi环境下推送,0为不限制网络环境,默认不限
|
|||
|
|
// 厂商下发策略;1: 个推通道优先,在线经个推通道下发,离线经厂商下发(默认);2: 在离线只经厂商下发;3: 在离线只经个推通道下发;4: 优先经厂商下发,失败后经个推通道下发;
|
|||
|
|
message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
|
|||
|
|
return message;
|
|||
|
|
}
|
|||
|
|
public static void main(String[] args){
|
|||
|
|
// * messageAppId:55c403f18356479b8e56c2042f3797fe
|
|||
|
|
// *
|
|||
|
|
// * producerId:178646581a6445a8acbeb6d8b0e6aac4
|
|||
|
|
// *
|
|||
|
|
// * sm2key:04258BA0F8E4F976F41247329708C45EDEA291A7740E0C4EC6CD4D82F6ECB9B6C66F3AF7339974DC62C27402D6DC93D7EBD763D2FD43A3A00677EF6A2683C3109C
|
|||
|
|
// *
|
|||
|
|
// * sm4key:66333929937495569688362944412811
|
|||
|
|
WxMsgConfig wxMsgConfig = new WxMsgConfig();
|
|||
|
|
// UAT环境配置
|
|||
|
|
wxMsgConfig.setBaseUrl("https://igw.isgcc.net:18443/isgcc_message/server");
|
|||
|
|
// 生产环境配置
|
|||
|
|
// wxMsgConfig.setBaseUrl("https://id.sgcc.com.cn:10443/mobile-message");
|
|||
|
|
// TODO : 以下三个参数需要进行替换
|
|||
|
|
wxMsgConfig.setProducerId("178646581a6445a8acbeb6d8b0e6aac4");
|
|||
|
|
wxMsgConfig.setSm4Key("66333929937495569688362944412811");
|
|||
|
|
wxMsgConfig.setSm2PublicKey("04258BA0F8E4F976F41247329708C45EDEA291A7740E0C4EC6CD4D82F6ECB9B6C66F3AF7339974DC62C27402D6DC93D7EBD763D2FD43A3A00677EF6A2683C3109C");
|
|||
|
|
// 以下三个Bean 建议在项目中以单例形式存在
|
|||
|
|
MsgRequestClient requestClient = new MsgRequestClient(wxMsgConfig);
|
|||
|
|
// 消息中心查询client
|
|||
|
|
MsgQueryClient msgQueryClient = new MsgQueryClient(requestClient);
|
|||
|
|
// 消息中心发送client
|
|||
|
|
MsgSendClient msgSendClient = new MsgSendClient(requestClient);
|
|||
|
|
|
|||
|
|
// 构建发送消息参数
|
|||
|
|
SendMsgParams wechatMsg = new SendMsgParams();
|
|||
|
|
// TODO : 发送目标用户
|
|||
|
|
wechatMsg.setTouser("sgitg_wangjianxing");
|
|||
|
|
// 发送目标组织
|
|||
|
|
// wechatMsg.setToparty("目标组织");
|
|||
|
|
// 跳转链接地址
|
|||
|
|
wechatMsg.setUrl("https://www.baidu.com");
|
|||
|
|
wechatMsg.setTitle("111对接测试标题" + System.currentTimeMillis());
|
|||
|
|
wechatMsg.setType("text");
|
|||
|
|
wechatMsg.setContent("对接测试内容" + System.currentTimeMillis());
|
|||
|
|
// wechatMsg.setPicurl("https://www.baidu.com/img/PCjing_5e539553b5304b9f43deb8a0fc918e45.png");
|
|||
|
|
wechatMsg.setDescription("对接测试内容" + System.currentTimeMillis());
|
|||
|
|
// 目标应用appId
|
|||
|
|
wechatMsg.setMessageAppId("55c403f18356479b8e56c2042f3797fe");
|
|||
|
|
WxSendMsgResult wxSendMsgResult = msgSendClient.sendMsg(wechatMsg);
|
|||
|
|
System.out.println(wxSendMsgResult);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|