Erlang实现的百度云推送Android服务端实例

ç¾åº¦äºæ¨éå®æ¹å°åhttp://developer.baidu.com/wiki/index.php?title=docs/cplat/push

ç®åçä»ç»ä¸åçï¼

ããç¾åº¦äºæ¨éæ¯æIOSåAndroidçäºæ¨éãAndroidæ¯æçè¿ä¸éï¼ä½æ¯IOSä¸è¬å¾é¾è°éãç¾åº¦äºå¯¹äºIOSçæ¨éæ¥è¯´ï¼ä»åªæ¯åäºä¸ä¸ªä¸­é´ç代çï¼ä¸ºç¨æ·æä¾æ¥å£ï¼ä¼ç¹æ¯ä½¿ç¨ç¾åº¦äºæ¨éï¼AndroidåIOSå¯ä»¥ç»ä¸ç®¡çï¼ç¼ºç¹æ¯ï¼è°éä¸å®¹æï¼ç¨æ·çIOSè¯ä¹¦éè¦ä¸ä¼ éªè¯ï¼ä¼ç´æ¥æ´é²ç»ç¬¬ä¸æ¹ï¼å¹¶ä¸IOSçapnsæ¯æç¨æ·èªå·±æ建provideræå¡å¨ï¼ä¸apnsç´æ¥éä¿¡ã

ãã个人建议ï¼Android使ç¨ç¾åº¦äºæ¨éï¼IOSç使ç¨èªå·±æ建çproviderä¸IOSçapnsç´æ¥éä¿¡ãä¸IOSçapnsç´æ¥éä¿¡æ许å¤å®ç°ï¼JavaãPHPãobject-cãc++ï¼å¨æçå客中æä¸ç¯æ¯ä½¿ç¨erlangå®ç°çï¼æå´è¶£çå¯ä»¥ççã

ããAndroidå®ç°æ¨éåè½ç¾åº¦äºæèªå·±çdemoï¼å¤§å®¶å¯ä»¥æ ¹æ®å®ç½ï¼http://developer.baidu.com/wiki/index.php?title=docs/cplat/pushï¼ç步骤ï¼é¦å注åä¸ä¸ªç¾åº¦å¼åèè´¦å·ï¼ç¶åå建èªå·±çä¸ä¸ªåºç¨ï¼æ ¹æ®ä¾å­ç¾åº¦äºæå¡å¨ä¼èªå·±ç产ä¸ä¸ªå®¢æ·ç«¯çdemoï¼å¯ä»¥è¿è¡å¨Androidçææºä¸ãç¶åå¨é¡µé¢è¿è¡æ¨éå®éªãå½ç¶ä¹å¯ä»¥ä¸è½½ç¾åº¦äºæ¨éæå¡å¨ï¼ç¶åéè¿è¿è¡ä½¿ç¨æå¡å¨æ¨éï¼å®ç½ä¸­æphpãJavaãPythonãnode.jsãc#å®ç°ã

ããé´äºéè¦ï¼ä¸ªäººä½¿ç¨erlangå®ç°çæå¡ç«¯æ¨éåè½ã

ãã步骤ï¼1ãé¦åå°ä¸é¢ä½ å»ºç«çå·¥ç¨ä¸­ç¾åº¦äºæå¡å¨ä¸ºä½ ç产çä¾å­demoå®è£å°ææºä¸ï¼ç¶å以èªå·±æ³¨åçå¼åèè´¦å·ç»éï¼éé¢æä¸ä¸ªuseridåchannelId,channelIdè®°å½ä¸æ¥ï¼UserIdåå¼åèä¸ç对åºä¸ä¸ï¼ç¶åå³å®ä½¿ç¨åªä¸ä¸ªï¼å¦æä¸æ ·ï¼é£æ²¡é®é¢ï¼ä¸ä¸æ ·çè¯ä¸è¬éç¨æ³¨åè页é¢ä¸­çé£ä¸ªãä¹å¯ä»¥éè¿Androidæå¡ç«¯è·åçæ°æ®åæåºuseridã

ãã2ãè·åtokenä¹å°±æ¯channelidï¼å·ä½è®¾å¤çidï¼

ãã3ãè·åç¨æ·çapikey

ãã4ãè·åç¨æ·çsecretekey

ããç»ç¨æ·åéæ¶æ¯åå ç§æåµåå«å¦ä¸ï¼ç»æ个ç¨æ·çææ设å¤åéä¿¡æ¯æ­¤æ¶éè¦ç¨æ·çuseridï¼ç»ç¨æ·çæ个设å¤åéä¿¡æ¯æ­¤æ¶éè¦ç¨æ·çuseridåchannelidï¼ç»æç±»ç¨æ·åéä¿¡æ¯ï¼éè¦tagåç±»ï¼ä¹å°±æ¯ç»è¯¥ç±»çææç¨æ·åéä¿¡æ¯ã

ããç®å示ä¾ï¼ç»ç¨æ·æä¸è®¾å¤åéä¿¡æ¯

代码如下:

start() ->

ããããinets:start(),


ãããã{Mega,Sec,_} = now(),


ããããApiKey = "apikey=aghjklpoikmnbhjklpoijnbfd",%%ç¨æ·çAPIkey

ãããã%% æç设å¤

ãããã% Channel_Id = "channel_id=1234567890098765432",%% ç¨æ·çæä¸è®¾å¤channelidï¼ä¹å°±æ¯å®¢æ·ç«¯demoå®è£å¨çææºçchannelid

ãããã%% 模æå¨

ããããChannel_Id = "channel_id=3959774938927755088",%%客æ·ç«¯æ¨¡æå¨çchannelid

ããããDevice_Type = "device_type=3",%%ç±»åï¼ç»èå¨å®æ¹æ档中æ¥çï¼1ï¼æµè§å¨è®¾å¤ï¼2ï¼PC设å¤ï¼3ï¼Android设å¤ï¼4ï¼IOS设å¤ï¼5ï¼Windows phone设å¤

ããããMessage_Type = "message_type=1",%% 0ï¼æ¶æ¯ï¼1ï¼éç¥ï¼é»è®¤ä¸º0

ããããTitleStr = " :kkkk,åååé¿ è¾¾èªç©º ä»¶é¿ æ¯é¡¿ï¼ï¼ï¼,ååå,erlang",

ããããDescriptionStr = "è¡ä¸:è¡å!",

ããããMessages0 = "messages={\"title\":\"", 

ããããMessages1 = TitleStr,

ããããMessages2 = "\",\"description\":\"",

ããããMessages3 = DescriptionStr,

ããããMessages4 = "\",\"notification_builder_id\":0,"

ãããã++ "\"notification_basic_style\":2,\"open_type\":2,\"url\":\"\",\"user_confirm\":0,\"pkg_content\":\"\",\"custom_content\":\"\"}",

ããããMessages = Messages0 ++ Messages1 ++ Messages2 ++ Messages3 ++ Messages4,

ããããMethod = "method=push_msg",

ããããMsg_keys= "msg_keys=erlang_keys",

ããããPush_Type = "push_type=1",

ããããTime = "timestamp="++ integer_to_list((Mega * 1000000 + Sec)),

ããããUser_Id = "user_id=213123123",%%ç¨æ·çuserid

ããããUrl = "http://channel.api.duapp.com/rest/2.0/channel/channel",

ããããHttp_Method = "POST",

ããããSecretKey = "123weqwe12wq12eqweqweqwe",%%ç¨æ·çsecretekey

ããããStr0 = Http_Method ++ Url ++ ApiKey ++ Channel_Id ++ Device_Type ++ Message_Type ++ Messages0,

ããããStr1 = Messages2,

ããããStr2 = Messages4 ++ Method ++ Msg_keys ++ Push_Type ++ Time ++ User_Id ++ SecretKey,

ããããTitleBin = list_to_binary(TitleStr),

ããããTIntegerU = unicode:characters_to_list(TitleBin,utf8),

ããããTChanges = utf8(TIntegerU,[]),

ããããTitle_Utf8 =

ããããããlists:foldl(fun(Change,Acc) ->

ããããããããcase Change of

ãããããããããã[Term] when Term >= $a, Term =< $z ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term >= $A, Term =< $Z ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term >= $0, Term =< $9 ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= $. ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= $_ ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= $- ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= 32 ->

ããããããããããããAcc ++ [43];

ãããããããããã_ ->

ããããããããããããAcc ++ list_to_hex_s(Change)

ããããããããend

ããããããend,[],TChanges),

ããããDesBin = list_to_binary(DescriptionStr),

ããããDIntegerU = unicode:characters_to_list(DesBin,utf8),

ããããDChanges = utf8(DIntegerU,[]),

ããããDes_Utf8 =

ããããããlists:foldl(fun(Change,Acc) ->

ããããããããcase Change of

ãããããããããã[Term] when Term >= $a, Term =< $z ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term >= $A, Term =< $Z ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term >= $0, Term =< $9 ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= $. ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= $_ ->

ããããããããããããAcc ++ Change;

ãããããããããã[Term] when Term =:= $- ->

ããããããããããããAcc ++ Change; 

ãããããããããã_ ->

ããããããããããããAcc ++ list_to_hex_s(Change)

ããããããããend

ããããããend,[],DChanges),

ããããStrValue = escape_uri(Str0) ++ Title_Utf8 ++ escape_uri(Str1) ++ Des_Utf8 ++ escape_uri(Str2),

ããããMd5Str = md5_hex(StrValue),

ããããSign = "sign=" ++ Md5Str,

ããããPostInfo = ApiKey ++ "&" ++ Sign ++ "&" ++ User_Id ++ "&" 

ãããã++ Channel_Id ++ "&" ++ Device_Type ++ "&" ++ Message_Type ++ "&" ++ Messages 

ãããã++ "&" ++ Method ++ "&" ++ Msg_keys ++ "&" ++ Push_Type ++ "&" ++ Time,

ããããtimer:sleep(30*1000),

ããããResult = httpc:request(post,{Url,[],"application/x-www-form-urlencoded",PostInfo},[],[]),

ããããcase Result of

ãããããã{ok,{{"HTTP/1.1",200,"OK"},_,_}} ->

ããããããããnothing;

ãããããã_ ->

ããããããããio:format("Result:~p~n",[Result])

ããããend.

相关推荐