首页 > 代码库 > erlang-百度云推送Android服务端功能实现-erlang

erlang-百度云推送Android服务端功能实现-erlang

百度云推送官方地址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 = http://www.mamicode.com/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.