腾讯推送——信鸽测试

因为需要用到服务器端推送信息,于是用了信鸽。在这里记录一下,顺便提一下几个我遇到的问题。这次测试我只用了单个账户的推送。前提是信鸽的access_id、secret_key和相关的设置都已经设置完毕。这个我略过,直接将服务器端开发测试

  • 时间戳的问题
  • sign签名的问题

    信鸽SDK说明文档中关于单个账户的推送
    url路径
    http://接口域名/v2/push/single_account?params
    请求参数:除了通用参数外,还包括如下参数
    参数名类型必需默认值描述

account string
针对某一账号推送,帐号可以是qq号,邮箱号,openid,手机号等各种类型
message_type uint
消息类型:1:通知 2:透传消息messagestring是无参见1.4、1.5两节
expire_time uint
天消息离线存储时间(单位为秒),最长存储时间3天。若设置为0,则使用默认值(3天)
send_time string
指定推送时间,格式为year-mon-day hour:min:sec 若小于服务器当前时间,则会立即推送
multi_pkg uint
表示按注册时提供的包名分发消息;1表示按access id分发消息,所有以该access id成功注册推送的app均可收到消息
environment uint
仅iOS必需无向iOS设备推送时必填,1表示推送生产环境;2表示推送开发环境。推送Android平台不填或填0响应结果:在通用返回结果参数中,result字段的json为空。本接口不返回push id
示例:MD5加密前url用作生成sign,RestApi Url为最终请求的url(以下为android推送示例,需替换通用参数后使用)
MD5加密前:
GETopenapi.xg.qq.com/v2/push/single_accountaccess_id=2100240957account=easonshipushtestaccountmessage={“title”:”测试消息”,”content”:”来自restapi的单个账号接口测试消息”}message_type=1timestamp=1502361241f255184d160bad51b88c31627bbd9530

RestApi Url:
http://openapi.xg.qq.com/v2/push/single_account?access_id=2100240957&account=easonshipushtestaccount&
message={“title”:”测试消息”,”content”:”来自restapi的单个账号接口测试消息”}&message_type=1&timestamp=1502361241&sign=0d7bee840e87801e8a90b831ee87eefb

我只设置了几个关键的参数,其他可空的参数都没设置。

这里注意的是,回到时间戳的问题。这里的时间戳是unix的时间戳
long longDate = System.currentTimeMillis()/1000;

签名的问题,需要注意签名加密前的字符串格式。在时间戳的后面加密钥。
如:
String get_skj = “GETopenapi.xg.qq.com/v2/push/single_accountaccess_id=”+access_id+”account=”+account+”message={‘content’:’”+message_content+”’,’title’:’”+message_title+”’}message_type=1timestamp=”
+unix_timestamp+secret_key;

这里直接加密钥,然后md5加密

String sign = MD5Kit.MD5(get_skj);

另外请求信鸽的getURL

String get_url = “http://openapi.xg.qq.com/v2/push/single_account“;
String sr = okGet(get_url+”?access_id=”+access_id+”&account=”+account+”
&timestamp=”+unix_timestamp+”&message_type=1&message={‘content’:’”+message_content+”’,’title’:’”+message_title+”’}&sign=”+sign);

在这里我用了OKhttp来get请求,发现比Apache的好用一些,代码也精简好多。
贴代码:

@Controller
public class HttpController {
    @RequestMapping(value="userTest",method=RequestMethod.GET)
    public @ResponseBody String userTest(){
           //发送GET请求
         //时间戳是unix 的long类型
        long longDate = System.currentTimeMillis()/1000;

        String account = "easonshipushtestaccount";
        String access_id = "2100240957";
        String unix_timestamp = String.valueOf(longDate);//long类型时间戳转成string类型
        String message_content = "okhttp测试成功!!";
        String message_title = "来自愤怒的柴";
        String secret_key = "255184d160bad51b88c31627bbd9530";

        String get_skj = "GETopenapi.xg.qq.com/v2/push/single_accountaccess_id="+access_id+"account="+account+"message={'content':'"+message_content+"','title':'"+message_title+"'}message_type=1timestamp="+unix_timestamp
        +secret_key;
        //签名md5加密
        String sign = MD5Kit.MD5(get_skj);
        String get_url = "http://openapi.xg.qq.com/v2/push/single_account";
        String sr = okGet(get_url+"?access_id="+access_id+"&account="+account+"&timestamp="+unix_timestamp+"&message_type=1&message={'content':'"+message_content+"','title':'"+message_title+"'}&sign="+sign);

        System.out.println(sr);
        return sr;
    }
/**
     * OKHttp
     */
    private static String okGet(String url){
        try {
            OkHttpClient httpClient = new OkHttpClient();
            Request request = new Request.Builder().url(url).build();
            Response reponse = httpClient.newCall(request).execute();
            return reponse.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

猜你喜欢

转载自blog.csdn.net/misseel/article/details/77711565
今日推荐