An Example of Baidu Cloud Push Android Server Implemented by Erlang

Time:2019-3-21

Baidu cloud push official address http://developer.baidu.com/wiki/index.php?Title=docs/cplat/push

The following principles are briefly introduced:

Baidu cloud push supports IOS and Android cloud push. Android support is good, but IOS is generally difficult to tune in. Baidu cloud for IOS push, he just acts as an intermediary agent, providing interface for users, the advantage is using Baidu cloud push, Android and IOS can be unified management; The disadvantage is: the call is not easy, users’IOS certificates need to upload validation, will be directly exposed to third parties, and IOS APNs support users to build their own provider server, and communicate directly with apns.

Personal Suggestion: Android uses Baidu cloud push, IOS uses its own built provider to communicate directly with IOS apns. There are many implementations of direct communication with IOS apns, Java, PHP, object-c, c++. One of my blogs is implemented using erlang. Interesting to see.

Baidu cloud has its own demo for Android push function. According to the steps of the official website (http://developer.baidu.com/wiki/index.php?Title=docs/cplat/push), you can register a Baidu developer account first, then create your own application. According to the example, Baidu cloud server will produce a client’s demo, which can run on Android’s mobile phone. Then the push experiment is carried out on the page. Of course, Baidu cloud push server can also be downloaded, and then through the operation of the server push, there are php, Java, Python, node. js, C # implementation in the official network.

In view of the need, personal use of Erlang to achieve the service-side push function.

Step: 1. First, install the demo of Baidu Cloud Server in the project you built above to your mobile phone. Then login with the developer account registered by yourself. There is a userid and channelId in the demo. ChannelId records them. UserId corresponds to the developer, and then decide which one to use. If the same, that’s no problem, note is usually used if it’s different. The one on the bookseller’s page. Userid can also be analyzed from data acquired by Android server.

2. Get token, or channelid, the ID of the specific device.

3. Getting apikey of users

4. Getting the secretekey of the user

Sending messages to users can be divided into several situations as follows: sending information to all devices of a user requires user’s userid; sending information to a device of a user requires user’s userid and channel id; sending information to a certain type of user requires tag classification, that is, sending information to all users of that type.

A simple example of sending information to a user’s device

Copy codeThe code is as follows:
start() ->

    inets:start(),

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

ApiKey = “apikey = aghjklpoikmnbhjklpoijnbfd”,%% user’s APIkey
%% My equipment
% Channel_Id= “channel_id=1234567890098765432”,%% of the users of a device channel id, that is, the client demo installed in the mobile phone channel ID
%% Simulator
Channel_Id = channel_id = 3959774938927755088″,%% client simulator channel ID
Device_Type= “device_type=3”,%% type, details are viewed in official documents, 1: browser device; 2: PC device; 3: Android device; 4: IOS device; 5: Windows phone device.
Message_Type = message_type = 1,%% 0: message; 1: notification; default 0
TitleStr = kkkk, Aston, Hahaha, erlang.
DescriptionStr = “Yes, 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”,%% user’s user ID
    Url = “http://channel.api.duapp.com/rest/2.0/channel/channel”,
    Http_Method = “POST”,
SecretKey = 123weqwe12wq12eqweqweqwe”,%% user’s 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.

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]