巴拉巴拉
小魔仙

个推——移动推送——消息透传

个推是一个国内的手机消息推送的第三方服务,和“极光(JPush)”是同一类服务。

可以快速的推送一些透传消息和顶栏消息链接消息什么的。

虽然官网有大量的案例和Demo,但是我这里还是做了一个相当于记录吧。

推送大概分为推送顶栏消息,链接消息和透传,我这里一直使用的是透传消息推送。
我这里都以透传为案例写了一个代码分享。

  • 代码分为给单个ClientId传递消息
  • 给单个Alias别面传递消息
  • 给ClientList列表传递消息
  • 给AliasList列表传递消息
  • 根据Tag传递消息

\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
好了,下面是代码时间:)

package com.againfly.util.getui;

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

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
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.TransmissionTemplate;

public class GeTuiPush {
	private static String GEXIN_APPID = "TxzlIyCcfS9KuENjjP4ux1";
	private static String GEXIN_APPKEY = "rAnoicfrNX7915IxPocAL2";
	private static String GEXIN_MASTERSECRET = "KFDNBNKAVj9bgykwvqgeA5";

	private static String GEXIN_URL = "http://sdk.open.api.igexin.com/apiex.htm";

	/**
	 * 给一个别名推送一条透传消息
	 */
	public static String PushToAlias(String title, String content, String alias) throws Exception {
		return PushMessage(title, content, alias, true);
	}
	
	/**
	 * 给一个CID推送一条透传消息
	 */
	public static String PushToClientId(String title, String content, String clientId) throws Exception {
		return PushMessage(title, content, clientId, false);
	}
	
	/**
	 * 根据别名列表推送透传消息
	 */
	public static String PushToAliasList(String title, String content, List<String> aliasList) throws Exception {
		return PushMessageToList(title, content, aliasList, true);
	}
	
	/**
	 * 根据CID列表推送透传消息
	 */
	public static String PushToClientIdList(String title, String content, List<String> clientIdList) throws Exception {
		return PushMessageToList(title, content, clientIdList, false);
	}
	
	/**
	 * 根据Tag推送
	 */
	public static String PushToTag(String title, String content, List<String> tagList) throws Exception{
		IGtPush push = new IGtPush(GEXIN_URL, GEXIN_APPKEY, GEXIN_MASTERSECRET);
		TransmissionTemplate template = genTransmissionTemplate(content, title);
		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(GEXIN_APPID);
		message.setAppIdList(appIdList);
		//手机类型
		List<String> phoneTypeList = new ArrayList<>();
		//省份
		List<String> provinceList = new ArrayList<>();
		
		/*List<String> provinceList = new ArrayList<String>();
		provinceList.add("浙江");
		provinceList.add("上海");
		provinceList.add("北京");
		provinceList.add("33010000");//杭州市
		provinceList.add("51010000");//成都市
		message.setProvinceList(provinceList);*/
		
		//自定义tag
		//List<String> tagList = new ArrayList<>();

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

		IPushResult ret = push.pushMessageToApp(message, "任务别名_toApp");
		return ret.getResponse().toString();
	}
	
	/**
	 * 发送透传消息
	 */
	private static String PushMessage(String title, String content, String recipient, boolean isAlias) throws Exception{
		IGtPush push = new IGtPush(GEXIN_URL, GEXIN_APPKEY, GEXIN_MASTERSECRET);
		TransmissionTemplate template = genTransmissionTemplate(content, title);// 设定消息模式为透传
		SingleMessage msg = new SingleMessage();// 个人推送
		msg.setOffline(true);
		msg.setOfflineExpireTime(2 * 1000 * 3600);
		msg.setData(template);
		Target target = new Target();// 设定接收人
		target.setAppId(GEXIN_APPID);
		if(isAlias){
			target.setAlias(recipient);
		}else{
			target.setClientId(recipient);
		}
		try {
			IPushResult ret = push.pushMessageToSingle(msg, target);
			return ret.getResponse().toString();
		} catch (RequestException e) {
			String requstId = e.getRequestId();
			IPushResult ret = push.pushMessageToSingle(msg, target, requstId);
			return ret.getResponse().toString();
		}
	}
	

	/**
	 * 给一个别名用户组推送
	 */
	private static String PushMessageToList(String title, String content, List<String> recipientList, boolean isAlias) throws Exception{
//		System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
		IGtPush push = new IGtPush(GEXIN_URL, GEXIN_APPKEY, GEXIN_MASTERSECRET);
		TransmissionTemplate template = genTransmissionTemplate(content, title);// 设定消息模式为透传
		ListMessage messageList = new ListMessage();
		messageList.setData(template);
		messageList.setOffline(true);
		messageList.setOfflineExpireTime(24 * 1000 * 3600);
		
		List<Target> targets = new ArrayList<>();// 配置推送列表
		for(String recipient : recipientList){
			Target target = new Target();
			target.setAppId(GEXIN_APPID);
			if(isAlias){
				target.setAlias(recipient);
			}else{
				target.setClientId(recipient);
			}
			targets.add(target);
		}
		// taskId用于在推送时去查找对应的message
		String taskId = push.getContentId(messageList);
		IPushResult ret = push.pushMessageToList(taskId, targets);
		return ret.getResponse().toString();
	}
	
	

	/**
	 * 生成一个透传消息
	 */
	private static TransmissionTemplate genTransmissionTemplate(String content, String title) throws Exception {
		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(GEXIN_APPID);
		template.setAppkey(GEXIN_APPKEY);
		template.setTransmissionContent(content);
		template.setTransmissionType(2);
		// ios使用
		APNPayload apnpayload = new APNPayload();
		APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
		if (title.length() > 50) {
			title = title.substring(0, 50) + "……";
		}
		alertMsg.setBody(title);// 通知文本消息字符串

		apnpayload.setContentAvailable(1);// 推送直接带有透传数据
		// apnpayload.setBadge(0);//应用icon上显示的数字
		apnpayload.setSound("default");// 通知铃声文件名
		apnpayload.setAlertMsg(alertMsg);
		template.setAPNInfo(apnpayload);
		template.setTransmissionType(2);
		return template;
	}
}

 

赞(0) 打赏
如果文章对你有帮助,欢迎你来评价反馈。AgainFly » 个推——移动推送——消息透传

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  • Q Q(选填)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏