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);
        //设置ios
        template.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;
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟驿站ㅤ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值