Java之个推推送工具类

2023-10-08 10:30
文章标签 java 工具 推送 之个

本文主要是介绍Java之个推推送工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.集成jdk
方式一:本地下载,下载服务端SDK开发工具包,下载地址为:http://www.getui.com/download/docs/getui/server/GETUI_JAVA_SDK_4.1.0.5.zip
方式二:上传jar包到私库,项目引用依赖。

2.编写工具类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.xxx.push.dubbo.util;import com.alibaba.fastjson.JSONObject;
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.TransmissionTemplate;import com.gexin.rp.sdk.template.style.Style0;
import lombok.extern.slf4j.Slf4j;import java.util.Map;/*** 个推推送工具类** @author lxp* @date 2019 -09-16 10:56:04*/
@Slf4j
public class GeTuiUtil {//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置private static String appId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";private static String appKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";private static String masterSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";(具体appId、appKey、masterSecret,在个推官网 )/*** 单个用户android推送** @param cid* @param msg* @return* @author lxp* @date 2019 -09-18 09:25:51*/public static boolean sendMessageAndroid(String cid, String msg) {IGtPush push = new IGtPush(appKey, masterSecret);TransmissionTemplate template = new TransmissionTemplate();template.setAppId(appId);template.setAppkey(appKey);template.setTransmissionType(2);template.setTransmissionContent(msg);// STEP2:设置通知样式//Style0 style = new Style0();// 设置通知栏标题与内容//style.setTitle("系统消息");//style.setText(msg);// 设置//template.setStyle(style);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 && ret.getResponse() != null && ret.getResponse().containsKey("result")) {log.info(ret.getResponse().toString());if(ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")){return true;}}return false;}/*** 单个用户ios推送** @param* @return*/public static String sendMessageIos(String cId, String msg) {IGtPush push = new IGtPush(appKey, masterSecret);TransmissionTemplate template = new TransmissionTemplate();template.setAppId(appId);template.setAppkey(appKey);template.setTransmissionContent(msg);// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动template.setTransmissionType(2);//设置iostemplate.setAPNInfo(getAPNPayload(msg));SingleMessage message = new SingleMessage();message.setData(template);message.setOffline(true);//离线有效时间,单位为毫秒,可选message.setOfflineExpireTime(24 * 1000 * 3600);// 可选,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) {return ret.getResponse().toString();} else {log.error("【IOS】个推,服务器异常");return "";}}private static APNPayload getAPNPayload(String msg) {//封装透传内容Map<String, Object> map= (Map<String, Object>) JSONObject.parse(msg);APNPayload payload = new APNPayload();//在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字(设置角标)payload.setAutoBadge(String.valueOf(map.get("count")));payload.setContentAvailable(1);//简单模式APNPayload.SimpleMsg//payload.setAlertMsg(new APNPayload.SimpleAlertMsg(msg));//字典模式使用下者String body = (String) map.get("message");payload.setAlertMsg(getDictionaryAlertMsg(body));//添加附加信息payload.addCustomMsg("message",msg);return payload;}private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String body) {APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();// iOS8.2以上版本支持alertMsg.setTitle("系统消息");alertMsg.setBody(body);return alertMsg;}
}

这篇关于Java之个推推送工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/164631

相关文章

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

基于Python打造一个全能文本处理工具

《基于Python打造一个全能文本处理工具》:本文主要介绍一个基于Python+Tkinter开发的全功能本地化文本处理工具,它不仅具备基础的格式转换功能,更集成了中文特色处理等实用功能,有需要的... 目录1. 概述:当文本处理遇上python图形界面2. 功能全景图:六大核心模块解析3.运行效果4. 相

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.