Android集成FCM(Firebace Cloud Messaging )

集成FCM官方文档

Firebace主页面

将 Firebase 添加到您的 Android 应用

1、进入Firebace页面,创建自己的项目

2、点击自己创建好的项目,在右侧选择Cloud Messaging

3、点击Android去创建 google-services.json

4、将下载的 google-services.json 文件移到您的模块(应用级)根目录中

5、添加相关SDK(根据自己AGP版本去添加对应的SDK版本,否则可能会编译失败)

将该插件作为依赖项添加到您的项目级 build.gradle 文件中:

根级(项目级)Gradle 文件 (<project>/build.gradle):

Groovy 复制代码
plugins {
  // ...

  // Add the dependency for the Google services Gradle plugin
  id 'com.google.gms.google-services' version '4.4.2' apply false

}

然后,在模块(应用级) build.gradle 文件中,同时添加要在您的应用中使用的 google-services 插件和任何 Firebase SDK:

模块(应用级)Gradle 文件 (<project>/<app-module>/build.gradle):

Groovy 复制代码
plugins {
  id 'com.android.application'

  // Add the Google services Gradle plugin
  id 'com.google.gms.google-services'

  ...
}

dependencies {
  // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:33.6.0')


  // TODO: Add the dependencies for Firebase products you want to use
  // When using the BoM, don't specify versions in Firebase dependencies
  implementation 'com.google.firebase:firebase-analytics'


  // Add the dependencies for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries

  implementation 'com.google.firebase:firebase-messaging:24.0.3'
}

重写FirebaseMessagingService

java 复制代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

    /**
     * 监听推送的消息
     * 三种情况:
     * 1,通知时:
     * 当应用处于前台的时候,推送的消息会走onMessageReceived方法,处于后台时走系统托盘。
     * 2,数据时:
     * 当应用处于前、后台的时候,会走onMessageReceived方法。
     * 3,通知且携带数据:
     * 当应用处于前台的时候,推送的消息会走onMessageReceived方法,处于后台时,通知走系统托盘,数据走Intent 的 extra 中(点击通知栏后)。
     */
    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {

    }

    /**
     * 当有新的Firebase token 时的回调
     * 第一次安装app 获取到的 pushtoken
     */
    @Override
    public void onNewToken(@NonNull String token) {
        //token 传递给后端
        //在Firebase控制台测试push的时候,需要将token配置进去
        Logger.i(TAG, "onNewToken =" + token);
    }

}
XML 复制代码
<service
            android:name="com.xx.xx.push.fcm.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

完成以上工作就可以正常接收到消息了

注意:系统设置中需要开启通知权限

控制台调试

进入Firebase Messaging页面,点击制作首个宣传活动

将设备上生成的token添加进去

问题

1、接收FCM消息需要进程存活,否则收不到

2、可以开启自启动权限,进程不在的时候收的FCM可以将进程拉起来

3、自测接受FCM消息的时候手机不需要翻墙

4、无法生成token的话,检查是不是没有安装Google play services服务

相关推荐
海水冷却1 天前
Android 群组聊天怎么实现:基于 ZIM 的完整落地指南(附可运行 Demo)
android
三少爷的鞋1 天前
现代 Android 架构指南:不要再主动设计 Callback 接口
android
TimeFine1 天前
AI 编码工作流的设计
android·ai编程
骑着蜗牛撵大象3271 天前
C++20 协程 × 屏障:手写一个 co_await 版 Barrier,让等待不再钉死线程
android·c++20·协程·并发编程·co_await·屏障同步
山东孙燕姿1 天前
设备台账重构:从一张表到三层模型
android·java·网络·低代码·重构·rxjava
蒸鱼Yuzheng1 天前
移动端 CPU 调度分析:线程亲和性、大小核迁移与卡顿
android·perfetto·性能分析·线程优化·cpu调度
booboosui1 天前
安卓唤醒词:12 维 MFCC 加 DTW,阈值 1.8
android
传奇开心果1 天前
Compose Multiplatform 共享边界实战:架构契约、互操作同步与可回退设计
android·kotlin
事圆则缓1 天前
从开机到首帧:Android 系统与应用启动流程源码分析
android
AI吃大瓜1 天前
指针压力表读数4:Android实现指针压力表读数(可实时检测)
android·指针读数·指针压力表·压力表·压力表读数