转载自 https://www.cnblogs.com/V1haoge/p/6439313.html

原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6439313.html

Java后台实现极光推送有两种方式,一种是使用极光推送官方提供的推送请求API:https://api.jpush.cn/v3/push,另一种则是使用官方提供的第三方Java SDK,这里先进行第一种方式推送的实现代码:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONArray;
import net.sf.json.JSONObject;
import sun.misc.BASE64Encoder;/*** java后台极光推送方式一:使用Http API* 此种方式需要自定义http请求发送客户端:HttpClient*/
@SuppressWarnings({ "deprecation", "restriction" })
public class JiguangPush {private static final Logger log = LoggerFactory.getLogger(JiguangPush.class);private String masterSecret = "xxxxxxxxxxxxxxxxxxxx";private String appKey = "xxxxxxxxxxxxxxxxxxx";private String pushUrl = "https://api.jpush.cn/v3/push";    private boolean apns_production = true;    private int time_to_live = 86400;private static final String ALERT = "推送信息";    /*** 极光推送*/public void jiguangPush(){String alias = "123456";//声明别名try{String result = push(pushUrl,alias,ALERT,appKey,masterSecret,apns_production,time_to_live);JSONObject resData = JSONObject.fromObject(result);if(resData.containsKey("error")){log.info("针对别名为" + alias + "的信息推送失败!");JSONObject error = JSONObject.fromObject(resData.get("error"));log.info("错误信息为:" + error.get("message").toString());}log.info("针对别名为" + alias + "的信息推送成功!");}catch(Exception e){log.error("针对别名为" + alias + "的信息推送失败!",e);}}/*** 组装极光推送专用json串* @param alias* @param alert* @return json*/public static JSONObject generateJson(String alias,String alert,boolean apns_production,int time_to_live){JSONObject json = new JSONObject();JSONArray platform = new JSONArray();//平台platform.add("android");platform.add("ios");JSONObject audience = new JSONObject();//推送目标JSONArray alias1 = new JSONArray();alias1.add(alias);audience.put("alias", alias1);JSONObject notification = new JSONObject();//通知内容JSONObject android = new JSONObject();//android通知内容android.put("alert", alert);android.put("builder_id", 1);JSONObject android_extras = new JSONObject();//android额外参数android_extras.put("type", "infomation");android.put("extras", android_extras);JSONObject ios = new JSONObject();//ios通知内容ios.put("alert", alert);ios.put("sound", "default");ios.put("badge", "+1");JSONObject ios_extras = new JSONObject();//ios额外参数ios_extras.put("type", "infomation");ios.put("extras", ios_extras);notification.put("android", android);notification.put("ios", ios);JSONObject options = new JSONObject();//设置参数options.put("time_to_live", Integer.valueOf(time_to_live));options.put("apns_production", apns_production);json.put("platform", platform);json.put("audience", audience);json.put("notification", notification);json.put("options", options);return json;}/*** 推送方法-调用极光API* @param reqUrl* @param alias* @param alert* @return result*/public static String push(String reqUrl,String alias,String alert,String appKey,String masterSecret,boolean apns_production,int time_to_live){String base64_auth_string = encryptBASE64(appKey + ":" + masterSecret);String authorization = "Basic " + base64_auth_string;return sendPostRequest(reqUrl,generateJson(alias,alert,apns_production,time_to_live).toString(),"UTF-8",authorization);}/*** 发送Post请求(json格式)* @param reqURL* @param data* @param encodeCharset* @param authorization* @return result*/@SuppressWarnings({ "resource" })public static String sendPostRequest(String reqURL, String data, String encodeCharset,String authorization){HttpPost httpPost = new HttpPost(reqURL);HttpClient client = new DefaultHttpClient();HttpResponse response = null;String result = "";try {StringEntity entity = new StringEntity(data, encodeCharset);entity.setContentType("application/json");httpPost.setEntity(entity);httpPost.setHeader("Authorization",authorization.trim());response = client.execute(httpPost);result = EntityUtils.toString(response.getEntity(), encodeCharset);} catch (Exception e) {log.error("请求通信[" + reqURL + "]时偶遇异常,堆栈轨迹如下", e);  }finally{client.getConnectionManager().shutdown();}return result;} /**     * BASE64加密工具    */public static String encryptBASE64(String str) {byte[] key = str.getBytes();BASE64Encoder base64Encoder = new BASE64Encoder();String strs = base64Encoder.encodeBuffer(key);return strs;}
}

  以上代码中使用的是第一种方式实现推送,下面介绍第二种方式:使用java SDK

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;/*** java后台极光推送方式二:使用Java SDK*/
@SuppressWarnings({ "deprecation", "restriction" })
public class JiguangPush {private static final Logger log = LoggerFactory.getLogger(JiguangPush.class);private static String masterSecret = "xxxxxxxxxxxxxxxxx";private static String appKey = "xxxxxxxxxxxxxxxx";private static final String ALERT = "推送信息";    /*** 极光推送*/public void jiguangPush(){String alias = "123456";//声明别名log.info("对别名" + alias + "的用户推送信息");PushResult result = push(String.valueOf(alias),ALERT);if(result != null && result.isResultOK()){log.info("针对别名" + alias + "的信息推送成功!");}else{log.info("针对别名" + alias + "的信息推送失败!");}}/*** 生成极光推送对象PushPayload(采用java SDK)* @param alias* @param alert* @return PushPayload*/public static PushPayload buildPushObject_android_ios_alias_alert(String alias,String alert){return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.alias(alias)).setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().addExtra("type", "infomation").setAlert(alert).build()).addPlatformNotification(IosNotification.newBuilder().addExtra("type", "infomation").setAlert(alert).build()).build()).setOptions(Options.newBuilder().setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数).setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
                        .build()).build();}/*** 极光推送方法(采用java SDK)* @param alias* @param alert* @return PushResult*/public static PushResult push(String alias,String alert){ClientConfig clientConfig = ClientConfig.getInstance();JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert);try {return jpushClient.sendPush(payload);} catch (APIConnectionException e) {log.error("Connection error. Should retry later. ", e);return null;} catch (APIRequestException e) {log.error("Error response from JPush server. Should review and fix it. ", e);log.info("HTTP Status: " + e.getStatus());log.info("Error Code: " + e.getErrorCode());log.info("Error Message: " + e.getErrorMessage());log.info("Msg ID: " + e.getMsgId());return null;}    }
}

  可以看出使用Java SDK实现推送的方式很简单,代码量也少,理解起来也不难,官方提供的SDK中将很多内容都实现了,我们只是需要配置一下信息,然后发起推送即可。需要注意的是使用第二种方式,需要导入极光官网提供的jar包。

  直接在maven中的pom文件中加入:

<dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.2.15</version>
</dependency>

  注意:在这里极有可能会出现jar包冲突:具体哪个包我也忘记了,好像是个日志包,找到后删除即可,我曾经记录过这个jar包冲突问题,详见jar包冲突解决。

  原本我们项目中也是采用第二种方式实现的,但是最后在提交代码时发现一个问题,那就是虽然我们只是带导入了官网提供的那个jar包,但是最后一统计,竟然无缘无故增多了80+个jar包,如此多的jar包提交过于臃肿,而且不现实,所以才临时改变方案,采用第一种方式进行编码。

  代码中采用的是别名方式进行推送,需要在在手机APP端进行别名设置,最好就是在用户登录之后就设置好,这样只要用户登录一次,它的绑定别名就可以保存到极光服务器,而我们推送时,指定这个别名,就能将信息推送到对应用户的手机上。

  其实我们发起推送请求,只是将信息发送到了极光服务器之上,这个信息有一个保存时限,默认一天,只要用户使用手机APP登录系统,极光服务器就会将信息自动推送到对应别名的手机上,由此可见,信息并非由我们后台直接推送到手机,而是通过极光服务器这个中转站,而这正式极光的工作。

  注意:这里告知一个技巧,这个别名设置的时候,其实直接将用户ID设置为别名即可,既方便,又安全,不用再去想办法生成一个唯一的串来进行标识,甚至需要在后台数据库中用户表中新增字段。

极光推送经验之谈-Java后台服务器实现极光推送的两种实现方式相关推荐

  1. java map遍历_Java中Map集合的两种遍历方式

    Java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下Java中Map集合的两种遍历方式! 关于遍历Map集合的几 ...

  2. 小米推送java_idea + springboot 的java后台服务器通过小米推送

    public class XiaomiPush { // 1.小米推送(我只推送Android且只应用regId发起推送,所以下面只有推送Android的代码 private static final ...

  3. java手动注册filter,SpringBoot注册Filter的两种实现方式

    springboot版本:2.2.5 一.filter注册 springboot中添加filter有两种方式: 1.实现方法一 2.实现方法二 二.Springboot自带filter Springb ...

  4. uniapp使用unipush推送及java后台推送代码(含本地打包apk使用unipush推送)

    你懂的,又是项目用到了,作为程序猿义无反顾需要定时 "进化" ,硬头皮去写,虽然曾经作为android开发者写了很多的推送,但是uniapp的推送也是有所差异的,记录一下,以后留用 ...

  5. JAVA:线程总结及多线程实现的两种方法

    JAVA:线程总结 目录 目录 JAVA:线程总结 JAVA:线程总结 01_多线程(多线程的引入)(了解) 02_多线程(多线程并行和并发的区别)(了解) 03_多线程(Java程序运行原理和JVM ...

  6. 当前没有可用的服务器_调研Redis高可用两种方案

    导读:Redis是被广泛使用的基础软件之一.对于工程师和,架构师,运维人员来说,了解Redis的高可用方案和背后的原理,是必备的基础知识.本文作者深入分析了Redis高可用的方方面面,并且做了有效总结 ...

  7. 我的世界 服务器内切换账号密码是多少,我的世界OP查询玩家账号密码 两种解决方式...

    我的世界OP查询玩家账号密码 两种解决方式.我的世界服务器中OP想要查询玩家账号登录密码也不是不可以的,但是一般需要插件协助才能完成.目前两种方式可以实现密码查询. 我的世界服务器OP怎么查询玩家密码 ...

  8. java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z 两种解决方案

    java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z 两种解决方案 这个异常是在我们配置spring +  ...

  9. 在Java中实现SFTP协议文件传输的两种解决方案

    在Java中实现SFTP协议文件传输的两种解决方案 1.1 背景 1.2 关于 FTP /FTPS 1.3 关于SFTP 解决方案一:使用 JSch 库 解决方案二:使用sshj 库 这篇博文来聊聊在 ...

最新文章

  1. MySQL在登陆时出现ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)错误...
  2. linux RTX2080显卡驱动
  3. 私.Modbus测试_ZC01_TCP方式
  4. 并发译文翻译计划(二)
  5. 如何使用NodeManager来控制WebLogic Server
  6. python实现邮件客户端_利用python实现简单的邮件发送客户端示例
  7. C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)
  8. 2017.4.19 细胞分裂 思考记录
  9. HTML5 原生混合模式,html5 – Chrome中的Chrome css3混合混合模式错误
  10. 使用Eclipse调试Android Native Application---cocos2d-x + Eclipse + Android + ndk
  11. SNS类游戏cache server设计浅析
  12. SpringCloud实战微服务之——Ribbon详解
  13. 使用Nginx Upstream 部署 OpenERP
  14. Jsp之一 WEB应用程序概述
  15. 积分激活大数据生态圈 堪比虚拟货币
  16. 云笔记是干什么用的,看云笔记的优点和使用心得分享
  17. 萨满祭司专业技能100%全分析
  18. ArchiCAD与Revit深度对比
  19. 产品人应该点亮哪些技能树?如何夯实产品基本功?
  20. Linux 中查看网卡是否启用

热门文章

  1. 高性能服务器 - window篇
  2. 【Boost】boost库中function和bind一起使用的技巧(二)
  3. sscanf函数用法详解
  4. 监听以太网(四) Packet32函数SDK
  5. 从618大促看直播风口,电商玩家如何走得更远
  6. LiveVideoStackCon深圳-嵌入式音频开发
  7. Puffer:专注拥塞控制、带宽预测和ABR
  8. 中篇 | 多轮对话机器之话题意图识别
  9. shell编程报错:“syntax error near unexpected token `”
  10. linux最大文件句柄数量总结