百度云推送支持IOS和Android的云推送。Android支持的還不錯(cuò),但是IOS一般很難調(diào)通。百度云對(duì)于IOS的推送來說,他只是做了一個(gè)中間的代理,為用戶提供接口,優(yōu)點(diǎn)是使用百度云推送,Android和IOS可以統(tǒng)一管理;缺點(diǎn)是:調(diào)通不容易,用戶的IOS證書需要上傳驗(yàn)證,會(huì)直接暴露給第三方,并且IOS的apns支持用戶自己構(gòu)建provider服務(wù)器,與apns直接通信。
個(gè)人建議:Android使用百度云推送,IOS的使用自己構(gòu)建的provider與IOS的apns直接通信。與IOS的apns直接通信有許多實(shí)現(xiàn),Java、PHP、object-c、c++,在我的博客中有一篇是使用erlang實(shí)現(xiàn)的,有興趣的可以看看。
Android實(shí)現(xiàn)推送功能百度云有自己的demo,大家可以根據(jù)官網(wǎng)(http://developer.baidu.com/wiki/index.php?title=docs/cplat/push)的步驟,首先注冊(cè)一個(gè)百度開發(fā)者賬號(hào),然后創(chuàng)建自己的一個(gè)應(yīng)用,根據(jù)例子百度云服務(wù)器會(huì)自己生產(chǎn)一個(gè)客戶端的demo,可以運(yùn)行在Android的手機(jī)上。然后在頁面進(jìn)行推送實(shí)驗(yàn)。當(dāng)然也可以下載百度云推送服務(wù)器,然后通過運(yùn)行使用服務(wù)器推送,官網(wǎng)中有php、Java、Python、node.js、c#實(shí)現(xiàn)。
步驟:1、首先將上面你建立的工程中百度云服務(wù)器為你生產(chǎn)的例子demo安裝到手機(jī)上,然后以自己注冊(cè)的開發(fā)者賬號(hào)登陸,里面有一個(gè)userid和channelId,channelId記錄下來,UserId和開發(fā)者上的對(duì)應(yīng)一下,然后決定使用哪一個(gè),如果一樣,那沒問題,不一樣的話一般采用注冊(cè)者頁面中的那個(gè)。也可以通過Android服務(wù)端獲取的數(shù)據(jù)分析出userid。
給用戶發(fā)送消息分幾種情況分別如下:給某個(gè)用戶的所有設(shè)備發(fā)送信息此時(shí)需要用戶的userid;給用戶的某個(gè)設(shè)備發(fā)送信息此時(shí)需要用戶的userid和channelid;給某類用戶發(fā)送信息,需要tag分類,也就是給該類的所有用戶發(fā)送信息。
start() ->
inets:start(),
{Mega,Sec,_} = now(),
ApiKey = "apikey=aghjklpoikmnbhjklpoijnbfd",%%用戶的APIkey
%% 我的設(shè)備
% Channel_Id = "channel_id=1234567890098765432",%% 用戶的某一設(shè)備channelid,也就是客戶端demo安裝在的手機(jī)的channelid
%% 模擬器
Channel_Id = "channel_id=3959774938927755088",%%客戶端模擬器的channelid
Device_Type = "device_type=3",%%類型,細(xì)節(jié)在官方文檔中查看,1:瀏覽器設(shè)備;2:PC設(shè)備;3:Android設(shè)備;4:IOS設(shè)備;5:Windows phone設(shè)備
Message_Type = "message_type=1",%% 0:消息;1:通知;默認(rèn)為0
TitleStr = " :kkkk,嘎嘎嘎阿 達(dá)航空 件阿 斯頓,,,,哈哈哈,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.