erlang-Baidu Cloud Push Android server function implementation-erlang

Source: Internet
Author: User

Baidu Cloud Push official address Http://developer.baidu.com/wiki/index.php?title=docs/cplat/push

Simple introduction to the following principle:

Baidu Cloud push support for iOS and Android cloud push. Android support is good, but iOS is generally hard to get through. Baidu Cloud for iOS push, he just did an intermediary agent, to provide users with interface, the advantage is the use of Baidu Cloud push, Android and iOS can be unified management; The disadvantage is: it is not easy to pass, the user's iOS certificate needs upload verification, will be directly exposed to third parties, And iOS APNs enables users to build provider servers themselves, communicating directly with APNs.

Personal advice: Android uses Baidu Cloud Push, iOS uses its own built-in provider to communicate directly with iOS APNs. There are many implementations of APNs direct communication with iOS, Java, PHP, object-c, C + +, one in my blog is implemented using Erlang, interested to see.

Android implementation push function Baidu Cloud has its own demo, we can according to the official website (http://developer.baidu.com/wiki/index.php?title=docs/cplat/push) steps, First register a Baidu developer account, and then create their own application, according to the example of Baidu Cloud server will produce a client's demo, can run on Android mobile phone. Then push the experiment on the page. Of course, you can also download Baidu Cloud push server, and then run using the server push, the official website has PHP, Java, Python, node. js, C # implementation.

In view of the need, the individual uses Erlang to implement the service-side push feature.

Step: 1, first of all the above you set up the project Baidu Cloud Server for your production example of the demo installed on the mobile phone, and then login with their own registered developer account, there is a userid and channelid,channelid record down, UserID and developers on the corresponding, And then decide which one to use, if same, that's fine, not the same, usually the one on the registrant's page. You can also analyze the userid using the data obtained from the Android server.

2, the acquisition token is channelid, the specific device ID,

3, access to the user's Apikey

4, access to the user's Secretekey

To send a message to the user in several cases are as follows: Send information to a user of all devices at this time need user's userid; send a message to a device user's UserID and channelid, send information to a certain type of user, need tag classification, This is to send information to all users of the class.

A simple example that sends a message to a user on a device

Start ()

Inets:start (),

{Mega,sec,_} = Now (),

ApiKey = "apikey=AGHJKLPOIKMNBHJKLPOIJNBFD", the apikey of percent users
Percent of my device
% channel_id = "channel_id=1234567890098765432", a percentage of the user's device Channelid, that is, the client demo installed on the phone's channelid
Percent-percent simulator
channel_id = "channel_id=3959774938927755088", percent of the client emulator Channelid
Device_type = "device_type=3", type of percent, details in official documentation, 1: Browser device, 2:pc device, 3:android device, 4:ios device, 5:windows phone device
Message_type = "message_type=1", percent 0: message; 1: notification; default is 0
Titlestr = ": KKKK, Quack ADA air parts Aston,,,, hahaha, Erlang",
Descriptionstr = "Line no: Yes!",
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 of Percent user
URL = "Http://channel.api.duapp.com/rest/2.0/channel/channel",
Http_method = "POST",
Secretkey = "123weqwe12wq12eqweqweqwe", Secretekey of percent users
STR0 = Http_method + + URL + ApiKey + + channel_id + device_type + message_type + + Messages0,
STR1 = Messages2,
STR2 = Messages4 + + + + 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 (CHANGE,ACC),
Case Change of
>= $a, term =< $z
ACC + + + change;
>= $A, term =< $Z
ACC + + + change;
[term] When the term >= $, term =< $9-
ACC + + + change;
[term] When the term =:= $. -
ACC + + + change;
[term] When the term =:= $_
ACC + + + change;
[term] When the term =:= $-
ACC + + + change;
[term] when the term =:=
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 (CHANGE,ACC),
Case Change of
>= $a, term =< $z
ACC + + + change;
>= $A, term =< $Z
ACC + + + change;
[term] When the term >= $, term =< $9-
ACC + + + change;
[term] When the term =:= $. -
ACC + + + change;
[term] When the term =:= $_
ACC + + + change;
[term] When the 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
+ + "& + + +" & "+ + 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", $, "OK"},_,_}}
Nothing
_,
Io:format ("Result:~p~n", [Result])
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.