2013年7月19日 星期五

GCM client 試出來了,問題是server 如何傳送中文資料 (by c#)?

GCM client如果有按照google API console 開設好server key 及GCM enable

還有在 在 Cloud Console中 點「Get Started With Sample Application」 對

「mobile backedn app」開啟及設定, 並依需要改寫gcm_client (參考git 連結 2013/06/05版,2012版比較繁瑣還要自己架server)

應該都是可以連入 (取得client device Id就應該不是問題了 )

但,是如果從C#要推播訊息怎麼做?
其本上一堆範例是直接向 「http://android.googleapis.com/gcm/send」做請求,丟一堆參數就可以了
問題是卡在中文訊息的問題,這時雙方需要encode & decode一下了
先就c# console sample為例
==========================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Util;
namespace ConsoleGcmPushTest {
    class Program {
        static void Main(string[] args) {
            string deviceId = "APA91xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZ1iLspAdBM";
            string msg = "16:15 建國的腳步漸漸逼近!!\n咱得愛有信心!!";
            string result = SendNotification(deviceId, msg);
            Console.WriteLine("respone is " + result);
            Console.WriteLine("");
            Console.WriteLine("Press anykey to continue");
            Console.ReadKey();
        }

        public static string SendNotification(string deviceId, string message) {
            string GoogleAppID = "AIzaSyBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpNxo";
            var SENDER_ID = "685########7580";
            var value = System.Web.HttpUtility.UrlEncode(message, System.Text.UTF8Encoding.GetEncoding("UTF-8"));

            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

            string postData = "collapse_key=score_update&time_to_live=1080&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
            Console.WriteLine(postData);

            Encoding encoding = new UTF8Encoding ();
            Byte[] byteArray = encoding.GetBytes(postData);


            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();


            tReader.Close();
            dataStream.Close();
            tResponse.Close();
            return sResponseFromServer;
        }
    }
}
===========================以上是推播的動作===================
以下是手機收到訊息時如何解碼
====================in GcmBroadcastReceiver================
 public void onReceive(Context context, Intent intent){
       ...
       ...
       ....
       msg = new String( intent.getExtras().getString("message").getBytes(), "UTF-8");
     //這才是之前data.message的內容......
    
}
======================================================

以上,大家加油了

沒有留言:

張貼留言