我方项目须要支持客户端消息推送,iOS终端能够借由苹果自己的apns很方便的实现,可是对于Android来讲,必须集成第三方的SDK来处理。考虑到项目须要以及成本,咱们选择使用谷歌的FCM框架来实现,由于咱们项目针对消息的可到达率要求并不高,并且推送消息的性质是业务低关注度的,重点是咱们的项目仍是海外项目,因此一番评估以后发现FCM彻底可以知足咱们的需求。json
一、Firebase云消息传递服务器简介api
二、FCM服务器协议服务器
三、使用旧版HTTP协议发送app
四、使用HTTP方式构建服务器框架
五、请求受权说明工具
六、向旧版协议发送请求提供受权google
七、使用HTTP旧版协议模式发送请求url
八、举例spa
Capation&D的这个项目基于SpringBoot,JDK1.8进行开发。code
一、Maven依赖
1 <dependency> 2 <groupId>com.google.api-client</groupId> 3 <artifactId>google-api-client</artifactId> 4 <version>1.24.1</version> 5 </dependency>
二、编写工具类
1 /** 2 * @Project:captainad-supply-chain 3 * @PackageName:com.captainad.supply-chain.common.push 4 * @Author:Captainad 5 * @blogsite:https://www.cnblogs.com/captainad/ 6 * @DateTime:2018/8/9 15:52. 7 * @Description: 基于Google Firebase框架实现的实时消息推送功能,目前仅支持Android设备 8 */ 9 @Slf4j 10 @Component 11 public class FcmPushService { 12 13 @Autowired 14 private GetSetCacheService getSetCacheService; 15 16 @Autowired 17 private HttpAskInterface httpAskInterface; 18 19 /** 20 * 消息推送,推送的消息用于提示,而且点击提示消息将会跳转连接至指定页面 21 * A: Captain&D 22 * W: https://www.cnblogs.com/captainad/ 23 * @param deviceToken 24 * @param title 25 * @param body 26 * @param route 27 * @throws Exception 28 */ 29 @Async 30 public void push(final String deviceToken, final String title, final String body, 31 final String route, final Integer dataType, final String dataMsg) 32 throws Exception { 33 log.info("[START]开始推送FCM消息"); 34 // 请求标头 35 Map<String, String> requestHeader = new HashMap<>(); 36 requestHeader.put("Content-Type", "application/json; UTF-8"); 37 requestHeader.put("Authorization", "Bearer " + getAccessToken()); 38 39 // 请求体 40 JSONObject json = new JSONObject(); 41 42 JSONObject message = new JSONObject(); 43 message.put("token", deviceToken); 44 JSONObject data = new JSONObject(); 45 46 // 发送弹窗提示信息 47 if(!StringUtils.isEmpty(title) && !StringUtils.isEmpty(body)) { 48 JSONObject notification = new JSONObject(); 49 notification.put("title", title); 50 notification.put("body", body); 51 message.put("notification", notification); 52 53 data.put("route", route); 54 // flag: 0-无需跳转,1-须要跳转 55 data.put("routeFlag", StringUtils.isEmpty(route) ? "0" : "1"); 56 } 57 58 // 发送数据 59 if(!StringUtils.isEmpty(dataMsg)) { 60 data.put("dataType", String.valueOf(dataType)); 61 data.put("params", dataMsg); 62 } 63 64 message.put("data", data); 65 json.put("message", message); 66 67 log.info("请求json内容===> {}", json.toString()); 68 // https://fcm.googleapis.com/v1/projects/bluepay-tesla/messages:send 69 String fcmApiUrl = getSetCacheService.getConfigValue("fcm_api_path"); 70 HttpResponse httpResponse = httpAskInterface.synSendPost(fcmApiUrl, json.toString(), requestHeader); 71 log.info("fcm响应内容===> {}", httpResponse); 72 log.info("[END]推送FCM消息结束"); 73 } 74 75 /** 76 * 获取定时刷新的令牌 77 * A: Captain&D 78 * W: https://www.cnblogs.com/captainad/ 79 * @return 80 * @throws IOException 81 */ 82 private String getAccessToken() throws Exception { 83 String jsonPath = getSetCacheService.getConfigValue("fcm_access_token_json"); 84 URL url = new URL(jsonPath); 85 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 86 InputStream inputStream = conn.getInputStream(); 87 88 GoogleCredential googleCredential = GoogleCredential 89 .fromStream(inputStream) 90 .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging")); 91 googleCredential.refreshToken(); 92 if(inputStream != null) { 93 inputStream.close(); 94 } 95 return googleCredential.getAccessToken(); 96 } 97 98 }
咱们只须要申请一个Google开发者帐号以及自身企业的一些相关信息,就可以很方便的使用Firebase云消息传递(FCM)提供的众多消息传递选项和功能,上面基于项目的须要实现了Android支持的版本,值得说起的时候,给特定设备推送消息时,须要提早获取到设备的deviceToken,由于它指代了一台惟一特定的设备。另外,若是想批量发送消息的,能够自行扩展出来。
一、https://firebase.google.cn/docs/cloud-messaging/concept-options?hl=zh-cn