package com.nuvole.util;
|
|
import com.gexin.rp.sdk.base.IAliasResult;
|
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.SingleMessage;
|
import com.gexin.rp.sdk.base.impl.Target;
|
import com.gexin.rp.sdk.base.notify.Notify;
|
import com.gexin.rp.sdk.base.payload.APNPayload;
|
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.TransmissionTemplate;
|
import com.nuvole.common.domain.emnu.CommonResultEmnu;
|
import com.nuvole.common.domain.result.CommonResult;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
|
/**
|
* 推送工具类(个推)
|
*
|
* @Author: lc
|
* @Date: 2020/4/26 13:41
|
*/
|
|
public class GeTuiPush {
|
|
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
|
// private static String appId = "0TrCn76GcW8T4y3wediOn9";
|
private static String appId = "g1FYuSXD2x5HY2KfWswP19";
|
// private static String appKey = "FUPAiYeLSo5hbPIZrRDT21";
|
private static String appKey = "kkMqLLBGLd7wx0mhIbG9l5";
|
// private static String masterSecret = "ne5FG9hfS77RNyGAnQF5p1";
|
private static String masterSecret = "AEuOKMlvTY9aLrdjtPhzy7";
|
private static IGtPush igtPush = new IGtPush(url, appKey, masterSecret);
|
|
|
/**
|
* 为用户绑定别名
|
*
|
* @param clientId 客户端身份id
|
* @param alias 别名
|
* @Author: lc
|
* @Date: 2020/4/26 13:36
|
*/
|
public static CommonResult bindAlias(String clientId, String alias) {
|
|
IAliasResult ret = null;
|
try {
|
ret = igtPush.bindAlias(appId, alias, clientId);
|
System.out.println("is bindAlias res:" + ret.getResponse());
|
} catch (RequestException e) {
|
e.printStackTrace();
|
}
|
if (ret != null) {
|
Map res = ret.getResponse();
|
if ("ok".equals(res.get("result").toString())) {
|
return new CommonResult(res);
|
} else {
|
return new CommonResult(CommonResultEmnu.ERROR, res.get("error_msg").toString());
|
}
|
} else {
|
return new CommonResult(CommonResultEmnu.ERROR, "绑定个推别名时出现异常!");
|
}
|
}
|
|
|
//测试
|
public static void main(String[] args) {
|
|
bindAlias("0482fa489fe285cbb8b96054f81a0b4f", "123456789");
|
pushMessageToSingle("测试demo", "测试demo内容", 2, "123456789");
|
}
|
|
/**
|
* 单推(单个clientId/别名)
|
*
|
* @param title 标题
|
* @param content 内容
|
* @param type 类型【1.设备id(clientId) 2.别名】
|
* @param val 类型值(clientId/别名)
|
* @Author: lc
|
* @Date: 2020/4/26 13:31
|
*/
|
public static Map pushMessageToSingle(String title, String content, int type, String val) {
|
|
/*透传消息模版*/
|
AbstractTemplate template = getTransmissionTemplate(title, content);
|
|
// 单推消息类型
|
SingleMessage message = getSingleMessage(template);
|
Target target = new Target();
|
target.setAppId(appId);
|
if (type == 1) {
|
target.setClientId(val);
|
} else {
|
target.setAlias(val);//别名需要提前绑定
|
}
|
IPushResult ret = null;
|
try {
|
ret = igtPush.pushMessageToSingle(message, target);
|
System.out.println("is pushMessageToSingle res:" + ret.getResponse());
|
} catch (RequestException e) {
|
e.printStackTrace();
|
ret = igtPush.pushMessageToSingle(message, target, e.getRequestId());
|
}
|
if (ret != null) {
|
return ret.getResponse();
|
} else {
|
System.out.println("个推单条推送服务器响应异常");
|
return null;
|
}
|
}
|
|
|
/**
|
* 推送一个列表
|
*
|
* @param title 标题
|
* @param content 内容
|
* @parma type 类型【1.设备id(clientId) 2.别名】
|
* @parma jsonArray 类型值(clientId/别名)
|
* @Author: lc
|
* @Date: 2020/4/26 13:37
|
*/
|
public static Map pushMessageToList(String title, String content, int type, List<String> list) {
|
/*透传消息模版*/
|
AbstractTemplate template = getTransmissionTemplate(title, content);
|
|
ListMessage message = new ListMessage();
|
message.setData(template);
|
// 设置消息离线,并设置离线时间
|
message.setOffline(true);
|
// 离线有效时间,单位为毫秒,可选
|
message.setOfflineExpireTime(24 * 1000 * 3600);
|
message.setPriority(1);
|
String taskId = igtPush.getContentId(message);
|
// 配置推送目标
|
List targets = new ArrayList();
|
for (int i = 0; i < list.size(); i++) {
|
String cid = list.get(i);
|
Target target = new Target();
|
target.setAppId(appId);
|
if (type == 1) {
|
target.setClientId(cid);
|
} else {
|
target.setAlias(cid);
|
}
|
targets.add(target);
|
}
|
|
IPushResult ret = null;
|
try {
|
ret = igtPush.pushMessageToList(taskId, targets);
|
System.out.println("is pushMessageToList:" + ret.getResponse());
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if (ret != null) {
|
return ret.getResponse();
|
} else {
|
System.out.println("个推推送一个列表服务器响应异常");
|
return null;
|
}
|
}
|
|
|
/**
|
* 推送所有手机用户
|
*
|
* @param title 标题
|
* @param content 内容
|
* @Author: lc
|
* @Date: 2020/4/26 13:36
|
*/
|
public static Map pushMessageToApp(String title, String content) {
|
/*透传消息模版*/
|
AbstractTemplate template = getTransmissionTemplate(title, content);
|
|
// 单推消息类型
|
AppMessage message = new AppMessage();
|
message.setData(template);
|
message.setOffline(true);
|
message.setOfflineExpireTime(24 * 1000 * 3600); //离线有效时间,单位为毫秒,可选
|
//全量推送时希望能控制推送速度不要太快,缓减服务器连接压力,可设置定速推送。如果未设置则按默认推送速度发送
|
message.setSpeed(100); // 设置为100,含义为个推控制下发速度在100条/秒左右
|
List<String> appIdList = new ArrayList<String>();
|
appIdList.add(appId);
|
message.setAppIdList(appIdList);
|
|
IPushResult ret = null;
|
try {
|
ret = igtPush.pushMessageToApp(message);
|
System.out.println("is pushMessageToApp res:" + ret.getResponse());
|
} catch (RequestException e) {
|
e.printStackTrace();
|
ret = igtPush.pushMessageToApp(message, e.getRequestId());
|
}
|
if (ret != null) {
|
return ret.getResponse();
|
} else {
|
System.out.println("个推推送所有手机用户服务器响应异常");
|
return null;
|
}
|
}
|
|
/**
|
* 透传消息模版
|
*
|
* @param title 标题
|
* @param content 内容
|
* @Author: lc
|
* @Date: 2020/4/26 13:46
|
*/
|
private static TransmissionTemplate getTransmissionTemplate(String title, String content) {
|
TransmissionTemplate template = new TransmissionTemplate();
|
/*设置APPID与APPKEY*/
|
template.setAppId(appId);
|
template.setAppkey(appKey);
|
/*透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动*/
|
template.setTransmissionType(2);
|
template.setTransmissionContent(content);
|
/*ios消息推送*/
|
template.setAPNInfo(getAPNPayload());
|
|
Notify notify = new Notify();
|
// notify.setIntent("intent:#intent;action=android.intent.action.oppopush;package=cn.com.pconline.android.browser;component=cn.com.android.browser/cn.com.android.browser.push.MultivendorPushActivity;S.protocol=pconlinebrowser://information-article/8270922;end");
|
notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.liuqit.notebook/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=" + title + ";S.content=" + content + ";S.payload=" + content + ";end");
|
notify.setTitle(title);
|
notify.setContent(content);
|
/*第三方*/
|
template.set3rdNotifyInfo(notify);
|
return template;
|
}
|
|
private static SingleMessage getSingleMessage(AbstractTemplate template) {
|
SingleMessage message = new SingleMessage();
|
message.setData(template);
|
// 设置消息离线,并设置离线时间
|
message.setOffline(true);
|
// 离线有效时间,单位为毫秒,可选
|
message.setOfflineExpireTime(72 * 3600 * 1000);
|
message.setPriority(1);
|
// 判断客户端是否wifi环境下推送。1为仅在wifi环境下推送,0为不限制网络环境,默认不限
|
message.setPushNetWorkType(0);
|
return message;
|
}
|
|
private static APNPayload getAPNPayload() {
|
APNPayload payload = new APNPayload();
|
//在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
|
payload.setAutoBadge("+1");
|
payload.setContentAvailable(1);
|
//ios 12.0 以上可以使用 Dictionary 类型的 sound
|
payload.setSound("default");
|
payload.setCategory("$由客户端定义");
|
payload.addCustomMsg("由客户自定义消息key", "由客户自定义消息value");
|
|
//简单模式APNPayload.SimpleMsg
|
payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
|
// payload.setAlertMsg(getDictionaryAlertMsg()); //字典模式使用APNPayload.DictionaryAlertMsg
|
|
//设置语音播报类型,int类型,0.不可用 1.播放body 2.播放自定义文本
|
payload.setVoicePlayType(0);
|
|
return payload;
|
}
|
|
}
|