绪论
最近研究了一下个推的消息推送,记录一下。
首先引入5个jar包。
使用个推官网演示应用
主要代码
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.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
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;
public class PushMsg {
static PushMsg me = new PushMsg();
static String appId = "VoT36u3tds8M15ppeP7HG9";
static String appKey = "oRVwYdcvSv9Bi5Ux9QJ3I6";
static String masterSecret = "x0dk6oYZ2i7S99l1FEvBr8";
static String clientId = "95ac79fe19b25f37cd6b087cfaf8e79f";
// 个推接口地址
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
/**
* 消息通知
*/
private NotificationTemplate setTongZhi(){
// 在通知栏显示一条含图标、标题等的通知,用户点击后激活您的应用
NotificationTemplate template = new NotificationTemplate();
// 设置appid,appkey
template.setAppId(appId);
template.setAppkey(appKey);
// 穿透消息设置为,1 强制启动应用
template.setTransmissionType(1);
// 设置穿透内容
template.setTransmissionContent("我是普通消息通知1");
// 设置style
Style0 style = new Style0();
// 设置通知栏标题和内容
style.setTitle("推送提醒");
style.setText("我是普通消息通知2");
// 设置通知,响铃、震动、可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
// 设置
template.setStyle(style);
return template;
}
/**
* 消息透传
*/
private TransmissionTemplate setTouChuan(){
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent("我是透传信息1");
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
//在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
payload.setAutoBadge("+1");
payload.setContentAvailable(1);
payload.setSound("default");
payload.setAlertMsg(getDictionaryAlertMsg());
template.setAPNInfo(payload);
return template;
}
/**
* 消息设置
*/
private APNPayload.DictionaryAlertMsg getDictionaryAlertMsg() {
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setTitle("透传提醒");
alertMsg.setBody("我是透传信息2");
return alertMsg;
}
/* 对单个用户推送消息
* 1. clientId
* 2. 要传到客户端的 msg
* 2.1 标题栏:key = title,
* 2.2 通知栏内容: key = titleText,
* 2.3 穿透内容:key = transText
*/
public String pushTongZhi() {
// 代表在个推注册的一个 app,调用该类实例的方法来执行对个推的请求
IGtPush push = new IGtPush(host, appKey, masterSecret);
// 创建信息模板
NotificationTemplate template = setTongZhi();
//定义消息推送方式为,单推
SingleMessage message = new SingleMessage();
// 设置推送消息的内容
message.setData(template);
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 3600 * 1000);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
// 设置推送目标
Target target = new Target();
target.setAppId(appId);
// 设置clientId
target.setClientId(clientId);
// 获得推送结果
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("服务器响应异常");
}
/* 1. 失败:{result=sign_error}
* 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
* 3. 异常
*/
System.out.println("通知发送结果:" + ret.getResponse().toString());
return ret.getResponse().toString();
}
/* 对单个用户推送消息
* 1. clientId
* 2. 要传到客户端的 msg
* 2.1 标题栏:key = title,
* 2.2 通知栏内容: key = titleText,
* 2.3 穿透内容:key = transText
*/
public String pushTouChuan() {
// 代表在个推注册的一个 app,调用该类实例的方法来执行对个推的请求
IGtPush push = new IGtPush(host, appKey, masterSecret);
// 创建信息模板
TransmissionTemplate template = setTouChuan();
//定义消息推送方式为,单推
SingleMessage message = new SingleMessage();
// 设置推送消息的内容
message.setData(template);
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 3600 * 1000);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
// 设置推送目标
Target target = new Target();
target.setAppId(appId);
// 设置clientId
target.setClientId(clientId);
// 获得推送结果
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("服务器响应异常");
}
/* 1. 失败:{result=sign_error}
* 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
* 3. 异常
*/
System.out.println("透传发送结果:" + ret.getResponse().toString());
return ret.getResponse().toString();
}
}
调用一下
public class Main {
public static void main(String[] args) {
PushMsg.me.pushTongZhi();
//PushMsg.me.pushTouChuan();
}
}
通知发送结果:{result=ok, taskId=OSS-0914_480d2dab12f91e6e0ef2bc401f7406b8, status=successed_online}
透传发送结果:{result=ok, taskId=OSS-0914_cb373c43f5b1fc73c63509b67a8e27c5, status=successed_online}
这样就可以看出我们发送的消息会显示那个在哪里,从而设置好数据格式,展示具体信息。
(若有什么错误,请留言指正,3Q)