什么叫推送?
中文名稱:推送
英文名稱:push
定義:描述因特網內容提供者和因特網用戶之間工作方式的術語。“推送”指因特網內容提供者定期向預訂用戶“提供”數據。
項目中有可能會用到推送。如果自己寫一個的話,可是個耗時耗力的時,好在很多第三方公司都提供了推送服務,比如百度雲。我們可以在自己的程序中使用它。
百度雲推送
雲推送(Push)是百度開放雲向開發者提供的消息推送服務;通過利用雲端與客戶端之間建立穩定、可靠的長連接來為開發者提供向客戶端應用推送實時消息服務。
百度雲推送服務支持推送三種類型的消息:通知、透傳消息及富媒體;支持向所有用戶或根據標簽分類向特定用戶群體推送消息;支持更多自定義功能(如自定義內容、后續行為、樣式模板等);提供用戶信息及通知消息統計信息,方便開發者進行后續開發及運營。
百度hi官方技術討論群:1405944 QQ群:242190646
雲推送服務具有以下特點:
1. 增強用戶粘性
通過雲和端之間建立的長連接,可以實時的推送消息到達用戶端。保持與用戶的溝通,大大提升用戶活躍度和留存率。
2. 節約成本
在省電省流量方面遠超行業水平,基礎的消息推送服務永久免費,大大節省開發者推送的成本。
3. 穩定安全的推送
強大的分布式集群長期為百度各大產品線提供推送服務,保證消息推送服務的穩定、可靠。
-------------------------------------------------------------
好吧,讓我們看看如何來使用它。
百度雲推送分兩部分:web端和手機端。
我們先看下手機端如何做。
1.注冊百度賬戶
2.加入 百度開發者
3.創建應用
4.下載sdk
5.導入sdk包,開發應用
5.1 在AndroidManifest.xml 中注冊響應的receiver
5.2 在主窗體的oncreate中寫
PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, PushServiceUtils.getMetaValue(this, "api_key"));
5.3 編寫自己的receiver。處理 綁定的相關消息,推送的消息,通知欄點擊后的消息
public class MyPushMessageReceiver extends BroadcastReceiver { private static final String TAG = "BroadcastReceiver"; @Override public void onReceive(final Context context, Intent intent) { Log.d(TAG, ">>> Receive intent: \r\n" + intent); if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) { // 獲取消息內容 String message = intent.getExtras().getString( PushConstants.EXTRA_PUSH_MESSAGE_STRING); // 消息的用戶自定義內容讀取方式 Log.i(TAG, "onMessage: " + message); // 自定義內容的json串 Log.d(TAG, "EXTRA_EXTRA = " + intent.getStringExtra(PushConstants.EXTRA_EXTRA)); // 用戶在此自定義處理消息,以下代碼為demo界面展示用 Intent responseIntent = null; responseIntent = new Intent(PushServiceUtils.ACTION_MESSAGE); responseIntent.putExtra(PushServiceUtils.EXTRA_MESSAGE, message); responseIntent.setClass(context, MainActivity.class); responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(responseIntent); } else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) { // 處理綁定等方法的返回數據 // PushManager.startWork()的返回值通過PushConstants.METHOD_BIND得到 // 獲取方法 final String method = intent .getStringExtra(PushConstants.EXTRA_METHOD); // 方法返回錯誤碼。若綁定返回錯誤(非0),則應用將不能正常接收消息。 // 綁定失敗的原因有多種,如網絡原因,或access token過期。 // 請不要在出錯時進行簡單的startWork調用,這有可能導致死循環。 // 可以通過限制重試次數,或者在其他時機重新調用來解決。 int errorCode = intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE, PushConstants.ERROR_SUCCESS); String content = ""; if (intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT) != null) { // 返回內容 content = new String( intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT)); } // 用戶在此自定義處理消息,以下代碼為demo界面展示用 Log.d(TAG, "onMessage: method : " + method); Log.d(TAG, "onMessage: result : " + errorCode); Log.d(TAG, "onMessage: content : " + content); Toast.makeText( context, "method : " + method + "\n result: " + errorCode + "\n content = " + content, Toast.LENGTH_SHORT) .show(); Intent responseIntent = null; responseIntent = new Intent(PushServiceUtils.ACTION_RESPONSE); responseIntent.putExtra(PushServiceUtils.RESPONSE_METHOD, method); responseIntent.putExtra(PushServiceUtils.RESPONSE_ERRCODE, errorCode); responseIntent.putExtra(PushServiceUtils.RESPONSE_CONTENT, content); responseIntent.setClass(context, MainActivity.class); responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(responseIntent); // 可選。通知用戶點擊事件處理 } else if (intent.getAction().equals( PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) { Log.d(TAG, "intent=" + intent.toUri(0)); // 自定義內容的json串 String customData = intent .getStringExtra(PushConstants.EXTRA_EXTRA); Log.d(TAG, "EXTRA_EXTRA = " + intent.getStringExtra(PushConstants.EXTRA_EXTRA)); if (customData == null || "".equals(customData)) { return; } Intent aIntent = new Intent(); aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); aIntent.setClass( context, com.pdwy.wulianwang.mobile.main.notification.NotificationDetails_Activity.class); String title = intent .getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE); aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_TITLE, title); String content = intent .getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT); aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT, content); String detailContent = ""; try { org.json.JSONObject json = new JSONObject(customData); detailContent = json.getString("detailContent"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 保存在數據庫 NotifyDao dao = new NotifyDao(); int notifyId = dao.saveNotify(title, content, detailContent); // 向消息詳細頁發送內容 aIntent.putExtra("notify_id", notifyId); context.startActivity(aIntent); } } }
web端需要做什么
1.建立項目
2.下載skd,引入包 bccs-api-lib-1.0.jar
3.編寫代碼。
/* * @brief 推送單播通知(Android Push SDK攔截並解析) message_type = 1 (默認為0) */ // 1. 設置developer平台的ApiKey/SecretKey String apiKey = "xxxxxxxxxxxxxxxxxx"; String secretKey = "xxxxxxxxxxxxx"; ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey); // 2. 創建BaiduChannelClient對象實例 BaiduChannelClient channelClient = new BaiduChannelClient(pair); // 3. 若要了解交互細節,請注冊YunLogHandler類 channelClient.setChannelLogHandler(new YunLogHandler() { @Override public void onHandle(YunLogEvent event) { System.out.println(event.getMessage()); } }); try { // 4. 創建請求類對象 PushBroadcastMessageRequest request = new PushBroadcastMessageRequest(); request.setDeviceType(3); // device_type => 1: web 2: pc 3:android // 4:ios 5:wp // request.setMessage("Hello Channel"); // 若要通知, request.setMessageType(1); request.setMessage("{\"title\":\"Notify_title_danbo\",\"description\":\"Notify_description_content\"}"); //request.setMessage(notify.toString()); // 5. 調用pushMessage接口 PushBroadcastMessageResponse response = channelClient .pushBroadcastMessage(request); // 6. 認證推送成功 System.out.println("push amount : " + response.getSuccessAmount()); } catch (ChannelClientException e) { // 處理客戶端錯誤異常 e.printStackTrace(); } catch (ChannelServerException e) { // 處理服務端錯誤異常 System.out.println(String.format( "request_id: %d, error_code: %d, error_message: %s", e.getRequestId(), e.getErrorCode(), e.getErrorMsg())); }
上面的代碼就能發送一條通知到手機。支持自定義消息標題,描述,其他自定義內容。
--------------
選用百度是個比較簡單實現的方式。截止2013-9-12,我沒有找到相關的收費信息。本着學習的精神可以研究研究,不過應該也可以再實際項目中使用。
