当前位置: 移动技术网 > IT编程>开发语言>Java > 基于FCM的消息推送功能

基于FCM的消息推送功能

2019年05月16日  | 移动技术网IT编程  | 我要评论

需求背景

我方项目需要支持客户端消息推送,ios终端可以借由苹果本身的apns很方便的实现,但是对于android来说,必须集成第三方的sdk来处理。考虑到项目需要以及成本,我们选择使用谷歌的fcm框架来实现,因为我们项目针对消息的可到达率要求并不高,而且推送消息的性质是业务低关注度的,重点是我们的项目还是海外项目,所以一番评估之后发现fcm完全能够满足我们的需求。

文档分析

1、firebase云消息传递服务器简介

2、fcm服务器协议

3、使用旧版http协议发送

4、使用http方式构建服务器

5、请求授权说明

6、向旧版协议发送请求提供授权

7、使用http旧版协议模式发送请求

8、举例

项目运用

capation&d的这个项目基于springboot,jdk1.8进行开发。

1、maven依赖

1 <dependency>
2     <groupid>com.google.api-client</groupid>
3     <artifactid>google-api-client</artifactid>
4     <version>1.24.1</version>
5 </dependency>

2、编写工具类

 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,因为它指代了一台唯一特定的设备。另外,如果想批量发送消息的,可以自行扩展出来。

参考资料

1、https://firebase.google.cn/docs/cloud-messaging/concept-options?hl=zh-cn

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网