本文主要是介绍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之个推推送工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!