当前位置: 移动技术网 > IT编程>脚本编程>Erlang > Erlang实现的百度云推送Android服务端实例

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

2017年12月01日  | 移动技术网IT编程  | 我要评论
百度云推送官方地址 简单的介绍下原理:   百度云推送支持ios和android的云推送。android支持的还不错,但是ios一般很难调通。百度云对于ios的推送来说

百度云推送官方地址

简单的介绍下原理:

  百度云推送支持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,大家可以根据官网()的步骤,首先注册一个百度开发者账号,然后创建自己的一个应用,根据例子百度云服务器会自己生产一个客户端的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_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.

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网