【个推】后端java开发

申请应用:

申请之后,我们开发人员需要拿到的是

 

开发步骤:

1.在pom文件中引入个推依赖

<!--个推-->
		<dependency>
			<groupId>com.gexin.platform</groupId>
			<artifactId>gexin-rp-sdk-template</artifactId>
			<version>4.0.0.16</version>
		</dependency>
		<dependency>
			<groupId>com.gexin.platform</groupId>
			<artifactId>gexin-rp-sdk-http</artifactId>
			<version>4.0.1.17</version>
		</dependency>

2.个推可以使用cid对个别用户推送,也可以把cid绑定别名(客户端工作),用别名进行推送,我这里使用别名进行推送

package com.dingyi.common.util;

import com.alibaba.fastjson.JSONObject;
import com.dingyi.common.Message.PushMessage;
import com.dingyi.common.base.CommonConstant;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
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.base.uitls.AppConditions;
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;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

@Component
public class Getui2Util {
    private static Logger logger = LoggerFactory.getLogger(Getui2Util.class);

    private static String appId = "GI*************VqHPT4";
    private static String appKey = "7m*************iRvbMaA";
    private static String masterSecret = "CZ*************HMGWeW2";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";
    
    /**
     * 推送给特定ios/安卓用户 注意:不是直接通知,需要客户端处理
     *
     * @param pushMessage
     * @param alias
     */
    public void pushMessageToOne(PushMessage pushMessage, String alias) {

        IGtPush push = new IGtPush(host, appKey, masterSecret);
        TransmissionTemplate template = getTemplate(pushMessage);
        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.toString()); //使用cid来推送
        target.setAlias(alias);
        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("服务器响应异常");
        }

    }


    /**
     * 推送给特定安卓用户 注意:ios接收不到
     *
     * @param pushMessage
     * @param alias
     */
    public void pushMessageToAndriod(PushMessage pushMessage, String alias) {

        IGtPush push = new IGtPush(host, appKey, masterSecret);
        NotificationTemplate template = notificationTemplate(pushMessage);
        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.toString());
        target.setAlias(alias);
        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("服务器响应异常");
        }

    }


    /**
     * 把信息推送给安卓app内所有用户
     *
     * @param pushMessage
     */
    public void pushMessageToApp(PushMessage pushMessage, String appTaskName) {

        IGtPush push = new IGtPush(host, appKey, masterSecret);
        //模版可以替换 这里不是透传模版 ios收不到
        NotificationTemplate template = notificationTemplate(pushMessage);
        AppMessage message = new AppMessage();
        message.setData(template);

        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        //推送给App的目标用户需要满足的条件
        AppConditions cdt = new AppConditions();
        List<String> appIdList = new ArrayList<String>();
        appIdList.add(appId);
        message.setAppIdList(appIdList);
        //手机类型
        List<String> phoneTypeList = new ArrayList<String>();
        //省份
        List<String> provinceList = new ArrayList<String>();
        //自定义tag
        List<String> tagList = new ArrayList<String>();

        cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
        cdt.addCondition(AppConditions.REGION, provinceList);
        cdt.addCondition(AppConditions.TAG, tagList);
        message.setConditions(cdt);

        //这个任务的名称,不会被展示
        IPushResult ret = push.pushMessageToApp(message, appTaskName);
        System.out.println(ret.getResponse().toString());
    }


    /**
     * 安卓模版
     */
    public NotificationTemplate notificationTemplate(PushMessage pushMessage) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent("点击启动应用");
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(pushMessage.getTitle());
        style.setText(pushMessage.getMsg());
        // 配置通知栏图标
        //style.setLogo("icon.png");
        // 配置通知栏网络图标
        //style.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        return template;
    }


    /**
     * ios/android透传模版
     */
    public static TransmissionTemplate getTemplate(PushMessage pushMessage) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(pushMessage.getMsg());
        template.setTransmissionType(2);
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(1);
        payload.setSound("default");
        //payload.setCategory("$由客户端定义");

        //简单模式APNPayload.SimpleMsg
        //payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));

        //字典模式使用APNPayload.DictionaryAlertMsg
        payload.setAlertMsg(getDictionaryAlertMsg(pushMessage));

        // 添加多媒体资源
        /*payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
                .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
                .setOnlyWifi(true));*/
        //需要使用IOS语音推送,请使用VoIPPayload代替APNPayload
        // VoIPPayload payload = new VoIPPayload();
        // JSONObject jo = new JSONObject();
        // jo.put("key1","value1");
        //         payload.setVoIPPayload(jo.toString());
        //
        template.setAPNInfo(payload);
        return template;
    }

    /**
     * ios离线APNS配置
     *
     * @return
     */
    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(PushMessage pushMessage) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody(pushMessage.getMsg());
        //alertMsg.setActionLocKey("ActionLockey");
        //alertMsg.setLocKey("LocKey");
        //alertMsg.addLocArg("loc-args");
        //alertMsg.setLaunchImage("launch-image");
        // iOS8.2以上版本支持
        alertMsg.setTitle(pushMessage.getTitle());
        //alertMsg.setSubtitle("子标题");
        //alertMsg.setTitleLocKey("TitleLocKey");
        //alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }


}

3.PushMessage类

public class PushMessage extends BaseModel {

    private String id;

    private String to;

    private String title;

    private String image;

    private String msg;

    private Date timestamp;

    private String readed;

    private String reached;

    //离线=offline ;在线=online ;系统 = system
    private String type;

    //0=文本; 1=图片;2=音频; 3=视频; 4=简历; 5=职位; 6=约面; 7=房源; 8=求租 ;
    // 9=预约看房;10=名片; 20=招聘;22=租房;25=系统;55=已读未读; 77=登录消息; 99=action
    private String msgtype;

    private String destination;
}

 

客户端接收不到通知的几种可能:

1.andriod能接受消息,而ios不行,有可能模版选错,ios只能接收透传模板

2.关于透传模版,在客户端是没有任何提示的,透传消息个推SDK接收到后直接广播给客户端,不做任何处理,需要客户端自己去处理。确认客户端是否对透传消息进行处理

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值