[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;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章