1. 程式人生 > >百度雲推送javaSDK demo

百度雲推送javaSDK demo

百度雲推送巴拉巴拉巴拉

近階段由於PM同學的強烈要求,我們app服務端準備接入百度雲推送工程,那麼現在開始demo走起,本Demo主要是針對標籤推送。

下載百度雲推送SDK

網址:http://push.baidu.com/doc/java/api
找到javaSDK進行下載即可;

import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import
com.baidu.yun.push.client.BaiduPushClient; import com.baidu.yun.push.constants.BaiduPushConstants; import com.baidu.yun.push.exception.PushClientException; import com.baidu.yun.push.exception.PushServerException; import com.baidu.yun.push.model.PushMsgToTagRequest; import com.baidu.yun.push.model.PushMsgToTagResponse; /** * Created by New-Bee on 2017/2/16. */
public class PushTest { public static void main(String[] args) throws PushClientException, PushServerException { /** * 1. * 身份合法性驗證 */ String apiKey = "domotGdRGCLR2x20UujEZNIz"; // 建立應用時,會在應用裡面看到 String secretKey = "IglRgUVnCHyGhblwo6UWAtzkFxkEMRxR"
; // 同上 PushKeyPair pair = new PushKeyPair(apiKey, secretKey); /** * 2. * 建立一個BaiduPushClient物件來訪問官方釋出介面 * 引數說明: * pair : 通過apiKey和secretKey獲取PushKeyPair物件 * BaiduPushConstants.CHANNEL_REST_URL: * 為服務URL地址,注意,如果你的推送伺服器不可以訪問外網的話,那麼這個url是不能用的 * * */ BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL); /** * 3. * 註冊YunLogHandler物件,來獲取互動請求中的詳細資訊 * * */ pushClient.setChannelLogHandler(new YunLogHandler() { public void onHandle(YunLogEvent event) { System.out.println(event.getMessage()); } }); try { /** * 4. * 開始組裝按標籤組推送介面所需要的引數 * * addTagName:標籤名稱 * addMsgExpires:訊息失效時間(單位秒) * addMessageType: 1 :普通通知 ,2:透傳通知 * addMessage: 新增訊息體 * addDeviceType:裝置型別 3:android 4:IOS * */ PushMsgToTagRequest request = new PushMsgToTagRequest() .addTagName("xingge") .addMsgExpires(new Integer(3600)) .addMessageType(1) // .addSendTime(System.currentTimeMillis() / 1000 + 70). .addMessage("{\"title\":\"TEST\",\"description\":\"Hello Baidu push!\"}") .addDeviceType(3); /** * 5. * 開始推送訊息 * */ PushMsgToTagResponse response = pushClient.pushMsgToTag(request); /** * 6. * Http請求返回值解析 * */ System.out.println("msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime() + ",timerId: " + response.getTimerId()); } catch (PushClientException e) { if (BaiduPushConstants.ERROROPTTYPE) { throw e; } else { e.printStackTrace(); } } catch (PushServerException e) { if (BaiduPushConstants.ERROROPTTYPE) { throw e; } else { System.out.println(String.format( "requestId: %d, errorCode: %d, errorMsg: %s", e.getRequestId(), e.getErrorCode(), e.getErrorMsg())); } } } }

這裡需要注意一點

安卓與IOS的訊息體格式是不一樣的
詳細訊息介面說明請參照官方文件

Android

{  
    "title" : "hello" ,  
    "description": "hello world" // 必選  
    "notification_builder_id": 0, // 可選  
    "notification_basic_style": 7, // 可選  
    "open_type":0, // 可選  
    "url": "http://developer.baidu.com", // 可選  
    "pkg_content":"", // 可選  
    "custom_content":{"key":"value"},  // 自定義 
}

IOS

{
    "aps": {  
         "alert":"Message From Baidu Cloud Push-Service",
         "sound":"",  // 可選
          "badge":0,    // 可選
    },
    "key1":"value1",// 自定義
    "key2":"value2"
}

一些問題

  1. 訊息推送延時特別嚴重,與官方所說的實時並不一致;
  2. 在寫Demo過程當中發現,IOS與Android居然是分開的,個人覺得這塊對於RD來說應該是透明的,設計不太合理;
  3. IOS偶現訊息丟失現象,通過現象推測,個人覺得應該是訊息覆蓋或訊息在還沒推送到裝置時已經失效;

總結

Created with Raphaël 2.1.0雲服務雲服務呼叫端SDK介面進行繫結;繫結成功並返回channelId。 Created with Raphaël 2.1.0Java服務Java服務攜帶channelId以及裝置型別,呼叫服務端介面與自己的服務進行繫結繫結成功。 Created with Raphaël 2.1.0java服務java服務雲服務雲服務呼叫JavaSDK介面並把訊息推到雲端推送成功,攜帶msgId和sendTime返回推送失敗,攜帶requestId返回