[Push]百度消息推送的应用

推送技术是指通过客户端与服务器端建立长链接,客户端可以接收由服务器端不定时发送的消息,在客户机/服务器的应用程序中,推送技术能够向客户机传送数据而无需其发出请求,例如发送电子邮件。相比较而言,万维网却是基于拉技术(Pull Technology),因此客户机浏览器必须事先向网页发出请求,所需信息才能被传送过来。

手机推送服务的原理很简单,就是通过建立一条手机与服务器的连接链路,当有消息需要发送到手机时,通过此链路发送即可

/**
  * 初始化百度推送
 * @return
 */
public static BaiduPushClient initPushClient(){
	String apiKey=""; 
	String secretKey=""; //需要去百度云推送平台申请
	PushKeyPair pair = new PushKeyPair(apiKey, secretKey); //设置两个属性
	BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);//实例化BaiduPushClient对象

	pushClient.setChannelLogHandler(new YunLogHandler() {
		@Override
		public void onHandle(YunLogEvent arg0) {
		System.out.println(arg0.getMessage());
		}
	});
	return pushClient;
}

/**
 * 消息推送
 * @return
 */
public static int pushOnlyMessage(String content,int pushType,int devType){
	BaiduPushClient pushClient=initPushClient();    //得到百度推送
	PushMsgToSingleDeviceRequest request=new PushMsgToSingleDeviceRequest();
	request.addMsgExpires(new Integer(3600))	//消息有效时间
	request.addDeviceType(devType);  		// devType => 1: web 2: pc 3:android 4:ios 5:wp
	request.addChannelId("");	 		// 广播ID
	request.addMessageType(pushType);		//
	request.addMessage(content);
	try {
		PushMsgToSingleDeviceResponse response=pushClient.pushMsgToSingleDevice(request);
		System.out.println("推送成功"+response.getMsgId());
	} catch (PushClientException e) {
		e.printStackTrace();
	} catch (PushServerException e) {
		e.printStackTrace();
	}
	return 0;
}


实例代码:

//向安卓移动端推送
public String sendAndroidMessage(String messageTitle, String messageContent, String[] channelIds)throws PushClientException,PushServerException {
    	
    	//1. 创建PushKeyPair用于app的合法身份认证apikey和secretKey可在应用详情中获取
	PushKeyPair pair = new PushKeyPair(apiKeyForAndorid, secretKeyForAndorid);
	// 2. 创建BaiduPushClient,访问SDK接口
	BaiduPushClient pushClient = new BaiduPushClient(pair,BaiduPushConstants.CHANNEL_REST_URL);
	// 3. 注册YunLogHandler,获取本次请求的交互信息
	pushClient.setChannelLogHandler(new YunLogHandler() {
		@Override
		public void onHandle(YunLogEvent event) {
			log.debug(event.getMessage());
		}
	});
	try {
		// 4. 设置请求参数,创建请求实例
		//创建Android通知
		JSONObject notification = new JSONObject();
		notification.put("title", messageTitle);		//标题
		notification.put("description",messageContent);		//内容
		notification.put("notification_builder_id", 0);		//客户端自定义通知样式,如果没有设置默认为0
		notification.put("notification_basic_style", 4);	//只有notification_builder_id为0时才有效,才需要设置,
		notification.put("open_type", 2);			//点击通知后的行为(打开Url:1; 自定义行为:2:其它值则默认打开应用;)
		notification.put("pkg_content", "#Intent;launchFlags=0x10000000;"
					      + "component=com.gasj.cp/.ui.MessageInfoActivity;"
					      + "S.descrip="+messageContent+";"
					      + "B.ispush=true;S.title="+messageTitle+";end");			
		PushBatchUniMsgRequest request = new PushBatchUniMsgRequest()
					.addChannelIds(channelIds)			//频道
					.addMsgExpires(new Integer(3600))		//设置消息的有效时间,单位秒,默认3600*5.
					.addMessageType(1)				//通知类型 0 透传,1通知
					.addMessage(notification.toString())
					.addDeviceType(3)				//客户端类型 3,安卓 4 ios
					.addTopicId("BaiduPush");			// 设置类别主题			        
		// 5. 执行Http请求
		PushBatchUniMsgResponse response = pushClient.pushBatchUniMsg(request);		 
		// 6. Http请求返回值解析
		log.info(String.format("msgId: %s, sendTime: %d",response.getMsgId(), response.getSendTime()));
		result="已发送";
	} catch (PushClientException e) {
		log.error(e.getMessage());
		result="发送失败";
	} catch (PushServerException e) {
		log.error(String.format("requestId: %d, errorCode: %d, errorMessage: %s",e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
		result="发送失败";
	}
	return result;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值