您当前的位置: 首页 >  服务器

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4642博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

全网首发:OPPO推送:服务器端的参考代码,JAVA版

柳鲲鹏 发布时间:2018-08-01 13:36:52 ,浏览量:0

  近来调试OPPO推送,服务器没有参考代码,简直让人崩溃。于是反复测试,排列组合,找到了正确的办法。也许有朋友也会遇到类似问题,所以在这里把代码共享出来。节省时间不说,最主要心情好一些。

  这些代码经过测试,确定可以产生正确结果。输出文本参考另外一篇文章。

  祝顺利。

 

  1、获得鉴权令牌:

private static boolean refreshToken() throws IOException
{
    String timestamp = ""+System.currentTimeMillis();
    String sha256= sha256(APP_KEY+timestamp+APP_MASTER_SECRET);
    String msgBody = MessageFormat.format(
            "app_key={0}&sign={1}×tamp={2}",
            URLEncoder.encode(APP_KEY,   "UTF-8"),
            URLEncoder.encode(sha256,    "UTF-8"),
            URLEncoder.encode(timestamp, "UTF-8"));

    String response = PushManager.httpPost(URL_AUTH, msgBody);
    System.out.println("response="+response);
    if (response.indexOf("Not Allowed") > 0)
    {
        return false;
    }

    JSONObject obj = JSONObject.parseObject(response).getJSONObject("data");
    System.out.println("JSONObject="+obj);
    authToken = obj.getString("auth_token");
    tokenExpiredTime = 24*60*60*1000+obj.getLong("create_time") - 5*60*1000;
    return true;
}

 

  2、发送推送消息:

public static  void sendNcPushMessage(String token, String username, String mobile) throws IOException
{
    if (tokenExpiredTime  10 ? mobile.substring(0, 10) : mobile);
    notification.put("content",     mobile);//消息内容体

    JSONObject message = new JSONObject();
    message.put("target_type",      2);
    message.put("target_value",     token);
    message.put("notification",     notification);

    //postBody之后不需要再编码。
    String postBody = MessageFormat.format("auth_token={0}&message={1}",
            authToken,
            URLEncoder.encode(message.toString(), "UTF-8"));
    String response = PushManager.httpPost(URL_PUSH, postBody);
    System.out.println("sendNcPushMessage="+response);
}
关注
打赏
1665724893
查看更多评论
立即登录/注册

微信扫码登录

0.0643s