http://www.getui.com//cn/index.html在个推官网注册个推账号
获取webapp的appid,如上图
保存后如下图:
移动端登陆后,需要缓存移动端clientId信息,如下:
/**
* 缓存处理用户移动端clientId信息
*/
this.cacheUserClientId=function(){
var info = plus.push.getClientInfo();
var os=plus.os.name;
var clientid=info.clientid;
if(clientid.length>0){
pdResourceSvr.service("PROCESS_USER_CLIENTID","1",{"clientId":clientid,"os":os}).then(function(data){
console.info("已缓存缓存处理用户移动端clientId信息...");
},function(msg){
console.info("缓存处理用户移动端clientId信息失败...");
});
}
}
后台处理接口
<%
//用户提交过来的保存白名单申请信息
String clientId = request.getParameter("clientId");//
String os = request.getParameter("os");//系统
String curUserId=ActionHelper.getShareId();//获取当前登陆的用户
JSONObject object = new JSONObject();
QueryHelper helper = new QueryHelper("SELECT * FROM T_EMOP_USER_CLIENTID WHERE YHID = ? AND CLIENTID =? ");
helper.addParameter(Types.VARCHAR, curUserId);
helper.addParameter(Types.VARCHAR, clientId);
DynaBean bean = dao.getSingleRow(null, helper);
if(bean==null){//验证通过,则保存用户申请信息
helper.clearParameters();
helper.setHql("INSERT INTO T_EMOP_USER_CLIENTID (XH,YHID,CLIENTID,SBLX,ORGID,CJR,CJSJ) VALUES (?,?,?,?,?,?,?)");
helper.addParameter(Types.VARCHAR,UUID.randomUUID());
helper.addParameter(Types.VARCHAR,curUserId);
helper.addParameter(Types.VARCHAR,clientId);
helper.addParameter(Types.VARCHAR,os);
helper.addParameter(Types.VARCHAR,Configuration.getProperty("system.orgid",""));
helper.addParameter(Types.VARCHAR,ActionHelper.getShareName());
helper.addParameter(Types.DATE,new Date());
dao.executeSQL(helper);
object.put("status_code", "0");
object.put("message", "clientId保存成功");
}
out.print(object);
%>
执行以下脚本(oracle)
-- CREATE TABLE
CREATE TABLE T_EMOP_USER_CLIENTID
(
XH NVARCHAR2(50) NOT NULL PRIMARY KEY,
YHID NVARCHAR2(50),
CLIENTID NVARCHAR2(50),
SBLX NVARCHAR2(50),
ORGID NVARCHAR2(50),
CJR NVARCHAR2(50),
CJSJ DATE,
XGR NVARCHAR2(50),
XGSJ DATE
);
-- ADD COMMENTS TO THE COLUMNS
COMMENT ON COLUMN T_EMOP_USER_CLIENTID.XH
IS '序号';
COMMENT ON COLUMN T_EMOP_USER_CLIENTID.YHID
IS '用户ID';
COMMENT ON COLUMN T_EMOP_USER_CLIENTID.CLIENTID
IS '用户CLIENTID值';
COMMENT ON COLUMN T_EMOP_USER_CLIENTID.SBLX
IS '设备类型(ANDROID,IOS)';
pom.xml添加如下配置
commons-lang
commons-lang
2.5
com.google
protobuf-java
2.5.0
com.gexin
gexin-rp-sdk-base
4.0.0.12
com.gexin
gexin-rp-sdk-http
4.0.1.7
com.gexin
gexin-rp-sdk-template
4.0.0.6
org.apache.commons
commons-lang3
3.0
package com.szboanda.mobile.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.DynaBean;
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.Target;
import com.gexin.rp.sdk.base.impl.Transparent.Notification;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.szboanda.platform.util.dao.JdbcDAO;
import com.szboanda.platform.util.helper.QueryHelper;
import com.szboanda.platform.util.listener.SystemListener;
import com.szboanda.platform.util.resources.SystemParameter;
/**
* 移动端消息推送类
* @author 李文祥
* @date 2016年9月7日
*/
public class PushUtils {
@SuppressWarnings({ "unchecked", "rawtypes" })
//这里我是把需要用到的三个值放在了缓存里面,也可以直接从个推里面获取,这里写常量
private static Map map = (Map)SystemListener.getSysApplication().getAttribute("SYS_PARAM");
private static String appId = map.get("APP_ID").getCsz();
private static String appKey =map.get("APP_KEY").getCsz();
private static String masterSecret = map.get("APP_MATERSECRET").getCsz();
/**
* 根据用户ID,查询clientId
* 参数:用户数组
*/
public static List getUserCid(List userId){
List clientList=new ArrayList();
String tempStr="";
for(String u:userId){
tempStr+="'"+u+"',";
}
tempStr=tempStr.substring(0, tempStr.length()-1);
JdbcDAO dao = new JdbcDAO();
QueryHelper helper = new QueryHelper("SELECT * FROM T_EMOP_USER_CLIENTID WHERE YHID IN ("+tempStr+") ");
List userList = dao.getListValue(null, helper);
for(DynaBean user:userList){
if(user.get("CLIENTID")!=null && user.get("CLIENTID").toString().length()>0){
clientList.add(user.get("CLIENTID").toString());
}
}
return clientList;
}
/**
* 根据用户ID,查询clientId
* 参数,用户id
*/
public static List getUserCid(String userId){
List clientList=new ArrayList();
JdbcDAO dao = new JdbcDAO();
QueryHelper helper = new QueryHelper("SELECT * FROM T_EMOP_USER_CLIENTID WHERE YHID='"+userId+"' ");
List userList = dao.getListValue(null, helper);
for(DynaBean user:userList){
if(user.get("CLIENTID")!=null && user.get("CLIENTID").toString().length()>0){
clientList.add(user.get("CLIENTID").toString());
}
}
return clientList;
}
/**
* 推送消息 (适合分发给全部人员,例如版本更新)
* @param title 通知栏中的通知标题
* @param content 通知栏的通知内容
*/
public static void pushToApp(String title,String content){
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(title);
style.setText(content);
// 配置通知栏图标
style.setLogo("icon.png");
// 配置通知栏网络图标
style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
IGtPush push = new IGtPush(appKey, masterSecret);
List appIds = new ArrayList();
appIds.add(appId);
// 定义"AppMessage"类型消息对象,设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
AppMessage message = new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(1000 * 600);
push.pushMessageToApp(message);
}
/**
* 推送消息给多人
* @param List userId 接受消息的人员
* @param title 通知栏中的通知标题
* @param content 通知栏的通知内容
*/
//向个推服务器发送请求
public static void pushToUser(List userId,String title,String content) throws IOException{
String pushText="{title:'"+title+"',content:'"+content+"',payload:'通知去干嘛这里可以自定义'}";
List cids= getUserCid(userId);
IGtPush push = new IGtPush(appKey, masterSecret);
push.connect();
ListMessage message = new ListMessage();
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 3600 * 1000);
//推送内容,格式为{title:'通知标题',content:'通知内容',payload:'通知去干嘛这里可以自定义'}
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(pushText);
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
//payload.setBadge(0);
payload.setContentAvailable(1);
payload.setSound("default");
payload.setCategory("$由客户端定义");
//简单模式APNPayload.SimpleMsg
payload.setAlertMsg(new APNPayload.SimpleAlertMsg(content));
template.setAPNInfo(payload);
message.setData(template);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
List targets = new ArrayList();
for(String cid:cids){
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
targets.add(target);
}
//推送前通过该接口申请“ContentID”
String contentId = push.getContentId(message);
IPushResult ret = push.pushMessageToList(contentId, targets);
System.out.println(ret.getResponse().toString());
}
/**
* 推送消息给1个人
* @param String userId 接受消息的人员
* @param title 通知栏中的通知标题
* @param content 通知栏的通知内容
*/
//向个推服务器发送请求
public static void pushToUser(String userId,String title,String content) throws IOException{
String pushText="{title:'"+title+"',content:'"+content+"',payload:'通知去干嘛这里可以自定义'}";
List cids= getUserCid(userId);
IGtPush push = new IGtPush(appKey, masterSecret);
push.connect();
ListMessage message = new ListMessage();
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 3600 * 1000);
//推送内容,格式为{title:'通知标题',content:'通知内容',payload:'通知去干嘛这里可以自定义'}
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(pushText);
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
//payload.setBadge(0);
payload.setContentAvailable(1);
payload.setSound("default");
payload.setCategory("$由客户端定义");
System.out.println("消息内容--"+content);
//简单模式APNPayload.SimpleMsg
payload.setAlertMsg(new APNPayload.SimpleAlertMsg(content));
template.setAPNInfo(payload);
message.setData(template);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
List targets = new ArrayList();
for(String cid:cids){
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
targets.add(target);
}
//推送前通过该接口申请“ContentID”
String contentId = push.getContentId(message);
IPushResult ret = push.pushMessageToList(contentId, targets);
System.out.println(ret.getResponse().toString());
}
//IOS发送
public static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, Notification nPojo){
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setBody(title);
alertMsg.setTitle("移动校园");
alertMsg.setTitleLocKey("ccccc");
alertMsg.setActionLocKey("移动校园");
return alertMsg;
}
/**
* 测试
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//推送给1个人
// pushToUser("SYSTEM","通知公告","您有一个新的通知公告,请点击查看。");
//推送给多个人
//List users=new ArrayList();
// users.add("SYSTEM");
//users.add("wangwuwu");
// pushToUser(users,"版本更新","有新版本需要更新。");
//推送给APP
//pushToApp("任务预警","您有一个任务还有1天就要超期,请及时办理。");
}
}
在需要推送消息给移动的地方,参考如下调用:
目前支持三种方式
pushToApp : 推送个App, 例如:版本更新
pushToUser(String,String,String) :推送给某一个人,例如:某人的待办任务
pushToUser(List
有问题,可联系我。