Java — 个推(demo)
发布日期:2021-06-30 19:50:35 浏览次数:2 分类:技术文章

本文共 3337 字,大约阅读时间需要 11 分钟。

1. 工具

package p02;import java.util.Map;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.http.IGtPush;import com.gexin.rp.sdk.template.NotificationTemplate;import com.gexin.rp.sdk.template.style.Style0;/* * 个推工具包 * (peerslee, 2017.2.12) */public class AppPushUtils {	//定义常量, appId、appKey、masterSecret  在"个推控制台"中获得的应用配置	// 由IGetui管理页面生成,是您的应用与SDK通信的标识之一,每个应用都对应一个唯一的AppID	private static String appId = "";	// 预先分配的第三方应用对应的Key,是您的应用与SDK通信的标识之一。	private static String appKey = "";	// 个推服务端API鉴权码,用于验证调用方合法性。在调用个推服务端API时需要提供。(请妥善保管,避免通道被盗用)	private static String masterSecret = "";	// 构造器	public AppPushUtils(String appId, String appKey, String masterSecret) {		// 初始化类		this.appId = appId;		this.appKey = appKey;		this.masterSecret = masterSecret;	}	// 设置通知消息模板	/*	 * 1. appId	 * 2. appKey	 * 3. 要传送到客户端的 msg	 * 3.1 标题栏:key = title, 	 * 3.2 通知栏内容: key = titleText,	 * 3.3 穿透内容:key = transText 	 */	private static NotificationTemplate getNotifacationTemplate(String appId, String appKey, Map
msg){ // 在通知栏显示一条含图标、标题等的通知,用户点击后激活您的应用 NotificationTemplate template = new NotificationTemplate(); // 设置appid,appkey template.setAppId(appId); template.setAppkey(appKey); // 穿透消息设置为,1 强制启动应用 template.setTransmissionType(1); // 设置穿透内容 System.out.println(msg.get("title") + "::" + msg.get("titleText") + "::" + msg.get("transText")); template.setTransmissionContent(msg.get("transText")); // 设置style Style0 style = new Style0(); // 设置通知栏标题和内容 style.setTitle(msg.get("title")); style.setText(msg.get("titleText")); // 设置通知,响铃、震动、可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); // 设置 template.setStyle(style); return template; } // 对单个用户推送消息 /* * 1. cid * 2. 要传到客户端的 msg * 2.1 标题栏:key = title, * 2.2 通知栏内容: key = titleText, * 2.3 穿透内容:key = transText */ public IPushResult pushMsgToSingle(String cid, Map
msg) { // 代表在个推注册的一个 app,调用该类实例的方法来执行对个推的请求 IGtPush push = new IGtPush(appKey, masterSecret); // 创建信息模板 NotificationTemplate template = getNotifacationTemplate(appId, appKey, msg); //定义消息推送方式为,单推 SingleMessage message = new SingleMessage(); // 设置推送消息的内容 message.setData(template); // 设置推送目标 Target target = new Target(); target.setAppId(appId); // 设置cid target.setClientId(cid); // 获得推送结果 IPushResult result = push.pushMessageToSingle(message, target); /* * 1. 失败:{result=sign_error} * 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online} * 3. 异常 */ return result; }}
2. 测试

package p02;import java.util.HashMap;import java.util.Map;import com.gexin.rp.sdk.base.IPushResult;public class AppPushTest {	public static void main(String[] args) {		String appId = "EHFx0RmBuL9iuvh05WxJq9";		String appKey = "T8X5ijnhO8ACHWCPRphrO8";		String masterSecret = "hvzdfG6ZSh8uo2N7eTNqq8";				Map
msg = new HashMap<>(); msg.put("title", "peerslee, 新年快乐!"); msg.put("titleText", "O(∩_∩)O~,"); msg.put("transText", ""); String []cids = {}; AppPushUtils pushUtils = new AppPushUtils(appId, appKey, masterSecret); for(String cid : cids) { System.out.println("正在发送消息..."); IPushResult ret = pushUtils.pushMsgToSingle(cid, msg); System.out.println(ret.getResponse().toString()); } }}
3.

转载地址:https://lipenglin.blog.csdn.net/article/details/55001715 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java - 图片序列化
下一篇:Python - 静态IP池

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月10日 20时11分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

putty连接AWS配置(multimedia project) 2019-04-30
Hourglass Network 沙漏网络 (pose estimation姿态估计) 2019-04-30
OpenCV实战(二)——答题卡识别判卷 2019-04-30
目标检测神经网络的发展历程(52 个目标检测模型) 2019-04-30
Boundary loss 损失函数 2019-04-30
神经网络调参实战(一)—— 训练更多次数 & tensorboard & finetune 2019-04-30
tensorflow使用tensorboard进行可视化 2019-04-30
神经网络调参实战(二)—— activation & initializer & optimizer 2019-04-30
凸优化 convex optimization 2019-04-30
数据库索引 & 为什么要对数据库建立索引 / 数据库建立索引为什么会加快查询速度 2019-04-30
IEEE与APA引用格式 2019-04-30
research gap 2019-04-30
pytorch训练cifar10数据集查看各个种类图片的准确率 2019-04-30
Python鼠标点击图片,获取点击点的像素坐标 2019-04-30
路径规划(一) —— 环境描述(Grid Map & Feature Map) & 全局路径规划(最优路径规划(Dijkstra&A*star) & 概率路径规划(PRM&RRT)) 2019-04-30
神经网络调参实战(四)—— 加深网络层次 & 批归一化 batch normalization 2019-04-30
数据挖掘与数据分析(三)—— 探索性数据分析EDA(多因子与复合分析) & 可视化(1)—— 假设检验(μ&卡方检验&方差检验(F检验))&相关系数(皮尔逊&斯皮尔曼) 2019-04-30
RRT算法(快速拓展随机树)的Python实现 2019-04-30
路径规划(二) —— 轨迹优化(样条法) & 局部规划(人工势能场法) & 智能路径规划(生物启发(蚁群&RVO) & 强化学习) 2019-04-30
D*算法 2019-04-30