GCM 的升级版——FCM(firebase cloud messaging)
推送服务对于现在的应用并不陌生,前有挡在墙外的GCM,和在墙内的Jpush,功能上大家大同小异,近几年大大小小的推送sdk层出不穷(比如:XG Push,华为推送,阿里云推送)到底哪个才能成为未来的官方认证的推送一直没有答案。之前谷歌推出的GCM一直因为 某堵墙 的原因一直不能稳定的使用,不过随着2016 Google开发者大会 的召开,以及google 推出的中文开发者网站,都预示着Google 的新推送方案——FCM 即将到来。
FCM的到来希望能更好的整合现在推送sdk 混乱的生态环境。本文简单的讲述如何把FCM集成到你的app中.
首先登录到Firebase网站:https://firebase.google.com
然后登陆自己的账户,点击右上角的:“转到控制台”
之后就会看到firebase 控制台页面
点击新建项目创建一个firebase项目
创建完成firebase项目之后可以看到项目的操作界面:
之后就可以选择添加你的ios /Android/ web 应用了。因为我们前面说过说要将fcm集成进Android app中 所以我们选择中间的“将firebase添加到您的Android应用“
之后的步骤会引导你填写应用的详细信息,还会帮你下载一个.json的配置文件,以及如何在你的Android项目中配置
最后 一切都完成之后我们会在“设置” 下的 “云消息传递”看到服务器端所使用的秘钥
至此我们的准备工作 做的就差不多了
下面开始集成并编写 代码,以确保手机端能收到推送
在 Android Studio 中,将 FCM 依赖项添加至您的应用级 build.gradle 文件:
dependencies {compile 'com.google.firebase:firebase-messaging:9.6.1'
}
将以下内容添加至您应用的清单中:
一项可以扩展 FirebaseMessagingService
的服务。如果您希望在后台进行接收应用通知之外的任何消息处理,则必须添加此服务。要在前台应用中接收通知、接收数据负载以及发送上游消息等,您必须扩展此服务。
一项可以扩展 FirebaseInstanceIdService
的服务,用于处理注册令牌的创建、轮转和更新。如果要发送至特定设备或者创建设备群组,则必须添加此服务。 如果FCM对于 Android
应用的功能至关重要,应确保在清单中设置 android:minSdkVersion=”8” 或更高版本。这可确保 Android
应用无法安装在其不能正常运行的环境中
也就是在AndroidManifest.xml 下添加如下代码:
<service
android:name=".MyFirebaseMessagingService"><intent-filter><action android:name="com.google.firebase.MESSAGING_EVENT"/></intent-filter>
</service>...<service
android:name=".MyFirebaseInstanceIDService"><intent-filter><action android:name="com.google.firebase.INSTANCE_ID_EVENT"/></intent-filter>
</service>
剩下的呢就需要自己写了 ,首先是 写一个service 继承FirebaseInstanceIdService,这个service是用来刷新token的,我们要做的就是把新的token发送给后台。
token可以使用FirebaseInstanceId.getInstance().getToken()
方法来获取
public class FCMInstanceIDService extends FirebaseInstanceIdService{@Overridepublic void onTokenRefresh() {super.onTokenRefresh();String refreshedToken = FirebaseInstanceId.getInstance().getToken();Log.v("FCM----", refreshedToken);sendRefreshToken(refreshedToken);}public void sendRefreshToken(String refreshedToken){//这里是往自己app应用的后台发送刷新refreshedToken的api}
}
再就是 收到消息处理的service 继承FirebaseMessagingService
收到的消息会以RemoteMessage 类型在onMessageReceived方法中返回,我们要做的是取出信息并使用notification 显示出来
public class FCMMessagingService extends FirebaseMessagingService {Context context = this;@Overridepublic void onMessageReceived(RemoteMessage remoteMessage) {super.onMessageReceived(remoteMessage);notification(context, getString(R.string.app_name), remoteMessage.getNotification().getBody());}@Overridepublic void onDeletedMessages() {super.onDeletedMessages();}@Overridepublic void onMessageSent(String s) {super.onMessageSent(s);}@Overridepublic void onSendError(String s, Exception e) {super.onSendError(s, e);}protected void notification(Context iContext, String iTitle, String iMessage){NotificationManager notificationManager = (NotificationManager) iContext.getSystemService(Context.NOTIFICATION_SERVICE);Notification.Builder builder = new Notification.Builder(getApplicationContext());int notificationIcon = R.drawable.icon;CharSequence notificationTitle = iTitle;long when = System.currentTimeMillis();Intent intent = new Intent(this, PinLockedActivity.class);intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);int requestCode, notificationId;final Random random = new Random(System.currentTimeMillis());requestCode = random.nextInt();notificationId = random.nextInt();PendingIntent contentIntent = PendingIntent.getActivity(iContext, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(contentIntent).setSmallIcon(notificationIcon).setTicker(notificationTitle).setContentTitle(notificationTitle).setContentText(iMessage).setWhen(when).setAutoCancel(true);Notification notification = builder.getNotification();notification.defaults = Notification.DEFAULT_ALL;notificationManager.notify(notificationId, notification);}}
至此我们的FCM集成进Android应用已经完成了。可以同过firebase 控制台的“ notification”进行推送测试。
注意几点问题:
1,
build.gradle 中所有依赖的谷歌的服务版本必须都一样,
举个栗子
compile 'com.google.android.gms:play-services-identity:9.6.1'compile 'com.google.android.gms:play-servicesanalytics:9.6.1'compile 'com.google.firebase:firebase-core:9.6.1'compile 'com.google.firebase:firebase-messaging:9.6.1'
版本必须统一都是9.6.1,如果一个版本改成10.0.0其他的也要相应的更新
2,
收到notification的时候可能有的手机看不到icon,那是因为5.0之后谷歌把所有控制栏的图编都要求做成黑白的(但是有的手机就可以支持,该死的碎片化),最好的方案是把icon做成黑白的,或者设置 targetSdkVersion 19 也能看到彩色的图标。
3,
FCM毕竟还没完全进入墙内,所以只能墙外开花墙内香。想要获取到token 并且 推送都好用暂时还是需要翻墙的。(希望离正常使用的日子不远了)。
希望大家多多与我交流~毕竟很菜
以上~
GCM 的升级版——FCM(firebase cloud messaging)相关推荐
- 创建Firebase项目并接入Firebase推送: Firebase Cloud Messaging (FCM)
1.FCM简介: Firebase Cloud Messaging (FCM) 是一种跨平台消息传递解决方案,可供您可靠地传递消息,而且还是免费的服务.支持 Android,IOS,Web,Flutt ...
- Flutter FCM - Flutter集成Firebase Cloud Messaging(FCM)
Firebase Cloud Messaging Firebase Cloud Messaging(FCM)是一种跨平台的消息传递解决方案,可让您可靠地免费发送消息. 使用FCM,您可以通知客户端应用 ...
- firebase 推送_使用Firebase Cloud Messaging发送推送通知
firebase 推送 Ever wondered how does your smartphone receive and handle notifications whether be in fo ...
- Windows Azure NotificationHub+Firebase Cloud Message 实现消息推动(付源码)
前期项目一直用的是Windows azure NotificationHub+Google Cloud Message 实现消息推送, 但是GCM google已经不再推荐使用,慢慢就不再维护了, 现 ...
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
最近在网上查了关于很多Android消息推送的资料,其中主要有四种方法. 1) 使用GCM服务(Google Cloud Messaging) 2) 使用XMPP协议(Openfire + Spark ...
- Google Cloud Messaging(GCM)简介与基本使用
Google Cloud Messaging(GCM)简介与基本使用 官方文档参考:https://developers.google.com/cloud-messaging/gcm GCM简介 Go ...
- Google Cloud Messaging:可折叠消息
在上一篇文章中 ,我们研究了如何在Android应用程序中设置Google Cloud Messaging(GCM) . 我们了解到,GCM是一项免费服务,它使我们能够将来自云的消息推送到运行我们的A ...
- messaging_Google Cloud Messaging:可折叠消息
messaging 在上一篇文章中 ,我们研究了如何在Android应用程序中设置Google Cloud Messaging(GCM) . 我们了解到,GCM是一项免费服务,它使我们能够将云中的消息 ...
- Unity如何接入Firebase Cloud Message远程推送
一. 前言: 当一款游戏发展到一定阶段,必不可少的要接入推送SDK,推送功能,分为以下两种: 本地推送:玩家打开游戏后,给手机系统通知设置推送的倒计时,等游戏关闭后,倒计时结束时,手机会自动打开通知给 ...
最新文章
- 登顶CLUE榜单,腾讯云小微与腾讯AI Lab联合团队提出基于知识的中文预训练模型...
- NeurIPS 2021论文放榜!清华投稿90篇排名第5,北大第9
- “诺奖摇篮”贝尔实验室:从辉煌到衰败的百年沉浮
- MassTransit_契约的创建
- 谷歌浏览器插件,当前网页地址的二维码
- mysql 织梦 索引_Mysql索引详解 建立索引的优势劣势以及索引规范
- css : 使用浮动实现左右各放一个元素时很容易犯的错误
- pythonelectron桌面开发案例_electron vue桌面应用入门实例
- Fread 和fwrite的参数不同,返回值不同
- 骑士游历问题问题_骑士步行问题
- 文献阅读005【精读】
- B00004 atoi函数
- Bean的六种作用域
- Web:仿苹果官网首页HTML和CSS
- android webview静态方法,在android webview中加载静态页面
- 【TWS使用系列1】如何从TWS的自选列表中添加/删除自选股?
- thinkphp使用ajax、jquery、Mysql实现了简单的客户端通信功能
- 解决IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter
- switchport mode access
- flask内容学习第三天(flak中的csrf跨站请求)