安卓&ios&hbuild 个推推送经验分享2

上文说了个推的安卓和ios基础配置。接下来说说他们的主要代码实现。由于在具体使用中,我们一般会根据需要给不同的用户推送信息,所以需要根据手机型号以及clientid锁定手机。

一、获取手机标识
首先,每个用户下载了软件后,我们获取每部手机的clientid和型号并且传到后台,并且存到数据库中。
$.plusReady(function() {
//获取手机clientid
var clientid=plus.push.getClientInfo().clientid;
//获取手机型号
var os=plus.os.name;
})

二、后台代码实现推送

// STEP1:获取应用基本信息
private static String appId = "mrpcrCj4Gp7Q9yz3S6Xrx1";
private static String appKey = "vUocRU9R1b57hFDJcPpfG4";
private static String masterSecret = "4tuSYu9KXc74zEDymEgJ65";
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

/**
 * 单个用户Android推送(通知推送)
 * 
 * @param user接受消息的人员
 * @param title通知栏中的通知标题
 * @param content通知栏的通知内容
 */
@RequestMapping("sendMesToAndroid")
@ResponseBody
// 向个推服务器发送请求
public static void sendMesToAndroid(User user,
		String title, String content) throws IOException {
	// http://localhost:8080/hydro/SendToPhone/sendMesToOne?title=sdsd&content=fggdgfd
	String pushText = "{title:'" + title + "',content:'" + content
			+ "',payload:'通知去干嘛这里可以自定义'}";
	String clientid = user.getClientid();

	IGtPush push = new IGtPush(appKey, masterSecret);
	push.connect();
	ListMessage message = new ListMessage();
	message.setOffline(true);
	// 离线有效时间,单位为毫秒,可选
	message.setOfflineExpireTime(24 * 3600 * 1000);
	// 推送内容,格式为{title:'通知标题',content:'通知内容',payload:'通知去干嘛这里可以自定义'}

	TransmissionTemplate template = new TransmissionTemplate();
	template.setAppId(appId);
	template.setAppkey(appKey);
	template.setTransmissionContent(pushText);
	template.setTransmissionType(2);
	APNPayload payload = new APNPayload();
	// payload.setBadge(0);
	payload.setContentAvailable(1);
	payload.setSound("default");
	payload.setCategory("$由客户端定义");
	// 简单模式APNPayload.SimpleMsg
	payload.setAlertMsg(new APNPayload.SimpleAlertMsg(content));
	template.setAPNInfo(payload);
	message.setData(template);
	// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
	message.setPushNetWorkType(0);
	List<Target> targets = new ArrayList<Target>();
	Target target = new Target();
	target.setAppId(appId);
	target.setClientId(clientid);
	targets.add(target);

	// 推送前通过该接口申请“ContentID”
	String contentId = push.getContentId(message);
	IPushResult ret = push.pushMessageToList(contentId, targets);
	System.out.println(ret.getResponse().toString());
}

/**
 * 单个用户ios推送(透传&APNS)
 * 
 * @param user接受消息的人员
 * @param title通知栏中的通知标题
 * @param content通知栏的通知内容
 * @return
 */
@RequestMapping("sendMesToIos")
@ResponseBody
public static String sendMesToIos( User user,
		String title, String content) {
	String clientid = user.getClientid();
	String pushText = title + ":" + content;
	IGtPush push = new IGtPush(url, appKey, masterSecret);

	SingleMessage message = new SingleMessage();

	TransmissionTemplate template = new TransmissionTemplate();
	template.setAppId(appId);
	template.setAppkey(appKey);
	template.setTransmissionContent(pushText);
	// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
	template.setTransmissionType(2);
	APNPayload payload = new APNPayload();
	// 在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
	// payload.setAutoBadge("+1");
	payload.setAutoBadge("0");
	payload.setContentAvailable(1);
	// ios 12.0 以上可以使用 Dictionary 类型的 sound
	payload.setSound("default");
	payload.setCategory("$由客户端定义");
	// 简单模式APNPayload.SimpleMsg
	// payload.setAlertMsg(new
	// APNPayload.SimpleAlertMsg(""+map.get("content")));
	// 字典模式使用下者
	HashMap<String, Object> map = new HashMap<>();
	map.put("title", title);
	map.put("cid", clientid);
	map.put("payload", pushText);
	map.put("body", content);
	payload.setAlertMsg(getDictionaryAlertMsg(map));
	template.setAPNInfo(payload);

	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(clientid);
	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 {
		return "";
	}
}
}

三、前台代码实现推送
直接在登录页进行代码处理(注:透传,在创建通知栏消息的时候,如果用hbuild进行打包,则创建通知栏消息会创建一条就替换一条,也就是不能存在多个通知栏消息。百度说要用高版本的hbuildx进行打包就可以解决这个问题,亲测有效)

			$.plusReady(function() {
				//监听系统通知栏消息点击事件  
				plus.push.addEventListener('click', function(msg){  
					// alert("点击事件");
				    //处理点击消息的业务逻辑代码  
				}, false);  
				//监听接收透传消息事件  
				plus.push.addEventListener('receive', function(msg){  
					 if(msg.type=='receive'){
				     		 //创建窗口消息
					 		plus.push.createMessage(msg.content,null,null);
					 }
				    //处理透传消息的业务逻辑代码  
				    
				}, false);
				}

四、平台测试指定手机用户

  1. 测试获取的手机cid是否有效,是否在线等,如果在线,会显示该手机被分配到的deviceToken(测试ios证书用)
    在这里插入图片描述
    在这里插入图片描述
    1. 可以进行单个用户测试
      如果上一步骤显示用户在线,则可以进行单个用户测试
      在这里插入图片描述
    2. ios证书测试
      如果需要ios的apns通道,需要配置证书,以下是证书的测试方式。(deviceToken的获取看前文)在这里插入图片描述
      以上就是本人的个推推送使用经验。如果有什么不足,可以提出来一起交流。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章