項目ITP(三) 玩玩 服務端 到 app端

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   tar   

項目ITP(三) 玩玩 服務端 到 app端


歡迎一起討論,QQ群【編程之美】:365234583

前言

  系列文章:[傳送門]

  泡泡腳,寫寫部落格,規律生活,睡個好覺,待會看會書。

 

本文 

  上面講了二維碼產生,及 手機端掃一掃,大家有興趣去看看。

  今天我們講一下,百度雲推送平台。

  每天想著問題,問題只會慢慢的清晰。想著想著,慢慢模式就出來了。

模式

          

                    推送互動模式

  ①② 所指的是學生群體

  ③  所指的是教師

 

  ③ :教師可以基於http 給伺服器指示,提示伺服器進行操作(push...等);或是直接在web端進行操作

  ① :學生群體接受 push,或是直接查看某些通知,或是直接查看富文本,或是然後點擊進行(③步驟)

  ② : 學生基於http 從伺服器拉去資料

  ##ps:大家有好的點子 留言

 

百度雲推送平台

   百度雲推送服務的相關資訊,主要包括兩部分:

    1. 快速開發使用 Push 服務。

    2. 更多 Push 服務的開發及使用功能。

Android端
  1. 註冊百度帳號,並成為百度開發人員;
  2. 建立應用,擷取 API Key 及 Secret Key,請參考查看應用密鑰;
  3. 下載應用樣本;
  4. 把樣本(Android 項目)匯入 Eclipse 工程;
  5. 運行樣本應用;
  6. 登入管理主控台發送通知;
  7. 手機端接收通知。

 

詳細資料:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/guide

  下面是:

      

      

  #API Key :     應用標識,服務端綁定和推送都要用到
  #Secret Key :  應用私密金鑰,服務端推送時用到
  #APP ID:     這個id ,就是個id ,雖然也有唯一行,暫時沒什麼用
  #channel ID:  推送通道id,通常對應一台終端。同樣的一個app ,裝在手機A 和手機B上,channel id是不同的。
  #user id :      應用的使用者id,同一個使用者,可以在不同的終端上擁有同一個app 。user id 和 channel id 配合使用

服務端

  直接上代碼吧

package sedion.jeffli.wmuitp.util.baidu;import com.baidu.yun.channel.auth.ChannelKeyPair;import com.baidu.yun.channel.client.BaiduChannelClient;import com.baidu.yun.channel.exception.ChannelClientException;import com.baidu.yun.channel.exception.ChannelServerException;import com.baidu.yun.channel.model.PushBroadcastMessageRequest;import com.baidu.yun.channel.model.PushBroadcastMessageResponse;import com.baidu.yun.channel.model.PushUnicastMessageRequest;import com.baidu.yun.channel.model.PushUnicastMessageResponse;import com.baidu.yun.core.log.YunLogEvent;import com.baidu.yun.core.log.YunLogHandler;public class AndroidPushByBaiDuHelper{    private static String apiKey     = "xxx";    private static String secretKey  = "xxx";        /**     * 初始化     * @return     */    private static BaiduChannelClient initPushClient()    {        // 1. 設定developer平台的ApiKey/SecretKey        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());            }        });        return channelClient;    }            /**     * 推送廣播訊息(訊息類型為透傳,由開發方應用自己來解析訊息內容) message_type = 0 (預設為0)     * @param Content 推送內容     */    public static void pushBroadcastMessage(String Content)    {                BaiduChannelClient channelClient = initPushClient();        try         {            // 4. 建立請求類對象            PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();            request.setDeviceType(3);             // device_type => 1: web 2: pc 3:android            // 4:ios 5:wp            request.setMessage(Content);            // 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()));        }    }        /**     * 推送單播訊息(訊息類型為透傳,由開發方應用自己來解析訊息內容) message_type = 0 (預設為0)     * @param ChannelId    手機端     * @param content    推送內容     * @param UserId    手機端     */    public static void pushMessageSample(String content, long ChannelId,String UserId)    {        BaiduChannelClient channelClient = initPushClient();        try         {            //建立請求類對象            // 手機端的ChannelId, 手機端的UserId, 先用1111111111111代替,使用者需替換為自己的            PushUnicastMessageRequest request = new PushUnicastMessageRequest();            request.setDeviceType(3); // device_type => 1: web 2: pc 3:android                                      // 4:ios 5:wp            request.setChannelId(ChannelId);            request.setUserId(UserId);            request.setMessage(content);            // 5. 調用pushMessage介面            PushUnicastMessageResponse response = channelClient                    .pushUnicastMessage(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()));        }    }    }

#初始化

#推送廣播訊息(訊息類型為透傳,由開發方應用自己來解析訊息內容) message_type = 0 (預設為0)

* @param Content 推送內容

# 推送單播訊息(訊息類型為透傳,由開發方應用自己來解析訊息內容) message_type = 0 (預設為0)

* @param ChannelId 手機端
* @param content 推送內容
* @param UserId 手機端

總結

  算個工具類吧 沒別的。看書咯,加油大家

感謝及資源共用

    

    

    路上走來一步一個腳印,希望大家和我一起。

    感謝讀者!很喜歡你們給我的支援。如果支援,點個贊。

    知識來源: 百度雲平台


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.