/**
* Project Name:lzlmall-b2c-common
* File Name:Getui2Util.java
* Package Name:com.lzlmall.b2c.common.util
* Date:2018-8-1上午8:58:17
* Copyright (c) 2018,
*
*/
package com.lzlmall.b2c.wx.api.util;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
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.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.APNPayload.DictionaryAlertMsg;
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;
/**
* ClassName:Getui2Util
*
* @Title:
* @Description :各推
* @author 黄木彬
* @version
* @since JDK 1.7 Date: 2018-8-1 上午8:58:17 Copyright (c) 2018, 上海凌天信息科技有限公司
* @see
*/
public class Getui2Util
{
static String appId = "J2FuricR277j7oeGet7XQ2";
static String appKey = "Npp8mhsgpm90VamAJv4bo3";
static String masterSecret = "wkEVMhVLCl531AzAv8uA37";
static String CID = "5ab76c3da19b75e211a5ffc281216191";
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
protected static Logger logger = Logger.getLogger(Getui2Util.class);
public static LinkTemplate linkTemplateDemo() {
LinkTemplate template = new LinkTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
// 设置通知栏标题与内容
template.setTitle("你有邻之邻新的订单");
template.setText("你有邻之邻新的订单");
// 配置通知栏图标
template.setLogo("icon.png");
// 配置通知栏网络图标,填写图标URL地址
template.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
template.setIsRing(true);
template.setIsVibrate(true);
template.setIsClearable(true);
// 设置打开的网址地址
//template.setUrl("");
return template;
}
/**
*
* @Title pushCid
* @Description: 安卓推送
* @author 黄木彬
* @date: 2018-8-15 下午5:44:22
* @since JDK 1.7
*/
public static void pushCid(){
IGtPush push = new IGtPush(host, appKey, masterSecret);
LinkTemplate template = linkTemplateDemo();
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(appId);
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) {
System.out.println(ret.getResponse().toString());
} else {
System.out.println("服务器响应异常");
}
}
/**
*
* @Title pushtoapp
* @Description: (这里用一句话描述这个方法的作用).
* @author 黄木彬
* @date: 2018-8-1 上午9:34:57
* @since JDK 1.7
*/
public static void pushtoapp(String url)
{
IGtPush push = new IGtPush(url, appKey, masterSecret);
// 定义“点击链接打开通知模板,并设置标题,内容,链接”
LinkTemplate template = new LinkTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTitle("你有邻之邻新的订单");
template.setText("你有邻之邻新的订单");
template.setLogo("push.png");
template.setUrl("http://getui.com");
List
appIds.add(appId);
// 定义“AppMessage”类型信息对象,设置消息内部模板,发送得目标app列表
// 是否支持离线发送,一级离线消息有效期
AppMessage message = new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(1000 * 600);
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
}
/**
*
* @Title notificationTemplate
* @Description: 安卓模版
* @author 黄木彬
* @date: 2018-8-16 上午9:55:21
* @since JDK 1.7
*/
public NotificationTemplate notificationTemplate(PushMessage pushMessage)
{
NotificationTemplate template = new NotificationTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
template.setTransmissionType(1);
template.setTransmissionContent("点击启动应用");
Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle(pushMessage.getTitle());
style.setText(pushMessage.getMsg());
// 配置通知栏图标
// style.setLogo("icon.png");
// 配置通知栏网络图标
// style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
return template;
}
/**
*
* @Title pushMessageToOne
* @Description: 推送给特定ios/安卓用户 注意:不是直接通知,需要客户端处理
* @author 黄木彬
* @date: 2018-8-15 下午4:14:47
* @since JDK 1.7
* type 1订单 2付款 3退款的 4积分订单
* status 2待确定 7已取消
*
*/
public static void pushMessageToOne( String cid,String so_id,String status,int type,String money,String messages)//type:1 订单 2积分订单
{
PushMessage pushMessage = null;
if(type==1){
pushMessage = new PushMessage(messages,"请查看" );
}else if(type==2){
pushMessage = new PushMessage( money+"元", "您已收到");
}else if(type ==3){
pushMessage = new PushMessage( messages,"请查看");
}else if(type ==4){
pushMessage = new PushMessage( messages,"请查看");
}else if(type ==5){
pushMessage = new PushMessage( messages,"请查看");
}
IGtPush push = new IGtPush(host, appKey, masterSecret);
System.out.println("=============cid["+cid+"]"+"so_id["+so_id+"]"+"type["+type+"]");
logBefore(logger,"=============cid["+cid+"]"+"so_id["+so_id+"]"+"type["+type+"]");
//返回模板
TransmissionTemplate template = null;
if(type ==5){
template= getDadaTemplate(pushMessage,String.valueOf(type));
}else{
template= getTemplate(pushMessage,so_id,status,String.valueOf(type));
}
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(appId);
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)
{
System.out.println(ret.getResponse().toString());
}
else
{
System.out.println("服务器响应异常");
}
}
/**
*
* @Title getDadaTemplate 达达模板
* @Description: ios/android透传模版
* @author 黄木彬
* @date: 2018-8-16 上午9:54:46
* @since JDK 1.7
*/
private static TransmissionTemplate getDadaTemplate(PushMessage pushMessage,String type) {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
String aa ="{\"payload\":{\"type\":\""+type+"\"},\"title\":\""+pushMessage.getTitle()+"\",\"content\":\""+pushMessage.getMsg()+"\"}";//
template.setTransmissionContent(JSONObject.parseObject(aa).toString());
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
// 在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
payload.setAutoBadge("+1");
payload.setSound("default");
payload.addCustomMsg("type", type);
payload.addCustomMsg("title", pushMessage.getTitle());
payload.addCustomMsg("content", pushMessage.getMsg());
// 字典模式
payload.setAlertMsg(getDictionaryAlertMsg(pushMessage));
template.setAPNInfo(payload);
return template;
}
/**
*
* @Title getTemplate
* @Description: ios/android透传模版
* @author 黄木彬
* @date: 2018-8-16 上午9:54:46
* @since JDK 1.7
*/
public static TransmissionTemplate getTemplate(PushMessage pushMessage,String so_id,String status, String type)
{
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
String aa =null;
if(type.equals("3")){
aa ="{\"payload\":{\"type\":\""+type+"\",\"sor_id\":\""+so_id+"\",\"status\":\""+status+"\"},\"title\":\""+pushMessage.getTitle()+"\",\"content\":\""+pushMessage.getMsg()+"\"}";//
}else{
aa ="{\"payload\":{\"type\":\""+type+"\",\"so_id\":\""+so_id+"\",\"status\":\""+status+"\"},\"title\":\""+pushMessage.getTitle()+"\",\"content\":\""+pushMessage.getMsg()+"\"}";//
}
template.setTransmissionContent(JSONObject.parseObject(aa).toString());
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
// 在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
payload.setAutoBadge("+1");
payload.setSound("default");
//payload.addCustomMsg("payload", "{\"so_id\":\""+so_id+"\",\"status\":\""+status+"\"}");
if(type.equals("3")){
payload.addCustomMsg("sor_id", so_id);
}else{
payload.addCustomMsg("so_id", so_id);
}
payload.addCustomMsg("type", type);
payload.addCustomMsg("status", status);
payload.addCustomMsg("title", pushMessage.getTitle());
payload.addCustomMsg("content", pushMessage.getMsg());
// 字典模式
payload.setAlertMsg(getDictionaryAlertMsg(pushMessage));
template.setAPNInfo(payload);
return template;
}
/**
*
* @Title getDictionaryAlertMsg
* @Description: ios离线APNS配置
* @author 黄木彬
* @date: 2018-8-16 上午9:54:59
* @since JDK 1.7
*/
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(
PushMessage pushMessage)
{
DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg();
dictionaryAlertMsg.setBody(pushMessage.getMsg());//推送内容
dictionaryAlertMsg.setTitle(pushMessage.getTitle());//推送标题
dictionaryAlertMsg.setActionLocKey("ActionLockey");
dictionaryAlertMsg.setLocKey("LocKey");
dictionaryAlertMsg.addLocArg("loc-args");
dictionaryAlertMsg.setLaunchImage("launch-image");
dictionaryAlertMsg.setTitleLocKey("TitleLocKey");
dictionaryAlertMsg.addTitleLocArg("TitleLocArg");
return dictionaryAlertMsg;
}
public static void logBefore(Logger logger, String interfaceName) {
logger.info("");
logger.info("================================================");
logger.info(interfaceName);
}
public static void main(String[] args)
{
// Getui2Util.pushMessageToOne("972fbdc6012479ffae26ce7468693667", "438","2","1",1,"0","你有新的订单");
// Getui2Util.pushMessageToOne("d0e5a2f4478143366525e0c9d3f6f660", "438","2","1",1,"0");
// Getui2Util.pushMessageToOne("972fbdc6012479ffae26ce7468693667", "2","2","2",2,"1000.00");
// Getui2Util.pushMessageToOne("9aba933b586958bab017dcb373b3973e", "9","3","3",1,"3","10000000.00");
}
}