@capacitor-community/fcm

中文文档
8.2.2发布于 2026/09/29MITTypeScriptESM + CJS零依赖近 30 天下载 13.5万

Enable Firebase Cloud Messaging features for Capacitor apps

原始使用文档
文档目录21 节
  1. Capacitor FCM
  2. Maintainers
  3. Notice 🚀
  4. Installation
  5. Compatibility
  6. Notice
  7. API
  8. Usage
  9. Add Google config files
  10. iOS
  11. Android
  12. Certificate
  13. iOS setup
  14. Prevent auto initialization
  15. SPM setup (iOS 15.0+)
  16. Android setup
  17. Variables
  18. Prevent auto initialization
  19. Example
  20. License
  21. Contributors ✨


Capacitor FCM

@capacitor-community/fcm

Capacitor community plugin for enabling FCM capabilities


Maintainers

MaintainerGitHubSocial
Stewan Silvastewones@stewones

Notice 🚀

We're starting fresh under an official org. If you were using the previous npm package capacitor-fcm, please update your package.json to @capacitor-community/fcm. Check out changelog for more info.

Installation

Using npm:

npm install @capacitor-community/fcm

Using yarn:

yarn add @capacitor-community/fcm

Sync native files:

npx cap sync

Compatibility

PluginCapacitoriOS deployment targetAndroid minSdkVersion
8.1.0+815.024
7.x - 8.0.x714.023
6.x613.022
4.x - 5.x513.022
3.x413.022

Notice

This plugin is intended to be used combined with Capacitor API for Push Notifications. Capacitor only provides APN token whereas this plugin offers the possibility to work with FCM tokens and more.

API

methodinfoplatform
subscribeTosubscribe to fcm topicios/android
unsubscribeFromunsubscribe from fcm topicios/android
getTokenget fcm token to eventually use from a serverios/android
refreshTokenrefresh fcm token to get a new oneios/android
deleteInstanceremove local fcm instance completelyios/android
setAutoInitenable the auto initialization of the libraryios/android
isAutoInitEnabledcheck whether auto initialization is enabledios/android
eventinfoplatform
tokenReceivedfires with { token } when FCM issues or rotates the token. On Android, listen to PushNotifications' registration event instead; its value is the FCM tokenios

Usage

import { FCM } from '@capacitor-community/fcm';
import { PushNotifications } from '@capacitor/push-notifications';

// external required step
// register for push
await PushNotifications.requestPermissions();
await PushNotifications.register();

// now you can subscribe to a specific topic
FCM.subscribeTo({ topic: 'test' })
  .then(r => alert(`subscribed to topic`))
  .catch(err => console.log(err));

// Unsubscribe from a specific topic
FCM.unsubscribeFrom({ topic: 'test' })
  .then(() => alert(`unsubscribed from topic`))
  .catch(err => console.log(err));

// Get FCM token instead of the APN one returned by Capacitor
FCM.getToken()
  .then(r => alert(`Token ${r.token}`))
  .catch(err => console.log(err));

// Delete the old FCM token and get a new one
FCM.refreshToken()
  .then(r => alert(`Token ${r.token}`))
  .catch(err => console.log(err));

// Remove FCM instance
FCM.deleteInstance()
  .then(() => alert(`Token deleted`))
  .catch(err => console.log(err));

// Enable the auto initialization of the library
FCM.setAutoInit({ enabled: true }).then(() => alert(`Auto init enabled`));

// Check the auto initialization status
FCM.isAutoInitEnabled().then(r => {
  console.log('Auto init is ' + (r.enabled ? 'enabled' : 'disabled'));
});

Add Google config files

Navigate to the project settings page for your app on Firebase.

iOS

Download the GoogleService-Info.plist file. In Xcode right-click on the yellow folder named "App" and select the Add files to "App".

Tip: if you drag and drop your file to this location, Xcode may not be able to find it.

Android

Download the google-services.json file and copy it to android/app/ directory of your capacitor project.

Certificate

iOS setup

  • Install homebrew (once)
  • brew install cocoapods (once a time)
  • ionic start my-cap-app --capacitor
  • cd my-cap-app
  • mkdir www && touch www/index.html
  • npx cap add ios
  • npm install --save @capacitor-community/fcm
  • npx cap sync ios (always do sync after a plugin install)
  • npx cap open ios
  • sign your app at xcode (general tab)
  • enable remote notification capabilities
  • add GoogleService-Info.plist to the app folder in xcode
// (optional) turn off `swizzling` in the `info.plist`
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>

Tip: every time you change a native code you may need to clean up the cache (Product > Clean build folder) and then run the app again.

Prevent auto initialization

If you need to implement opt-in behavior, you can disable the auto initialization of the library by following the Firebase docs.

SPM setup (iOS 15.0+)

First ensure all your dependencies are compatible with SPM. Otherwise, stick to the Cocoapods installation steps.

You can also install the plugin using Swift Package Manager (SPM) instead of CocoaPods. To do so, you don't need to install CocoaPods (steps 1 & 2), but you'll need to configure capacitor to use SPM:

  • to add iOS target: npx cap add ios --packagemanager SPM
  • to migrate an existing target: npx cap spm-migration-assistant (with capacitor 7.4.0+).

You can find more info in the official capacitor docs.

Android setup

  • ionic start my-cap-app --capacitor
  • cd my-cap-app
  • mkdir www && touch www/index.html
  • npx cap add android
  • npm install --save @capacitor-community/fcm
  • npx cap sync android (always do sync after a plugin install)
  • npx cap open android
  • add google-services.json to your android/app folder

Now you should be set to go. Try to run your client using ionic cap run android --livereload.

Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.

Variables

This plugin will use the following project variables (defined in your app's variables.gradle file):

  • $firebaseMessagingVersion version of com.google.firebase:firebase-messaging (default: 25.0.1)

Prevent auto initialization

If you need to implement opt-in behavior, you can disable the auto initialization of the library by following the Firebase docs.

Example

License

MIT

Contributors ✨

Thanks goes to these wonderful people (emoji key):

stewones
stewones

💻 📖
Daniel Pereira
Daniel Pereira

💻 📖
Priyank Patel
Priyank Patel

💻
Nikolas
Nikolas

🚧
Ben Schattinger
Ben Schattinger

💻
James Manners
James Manners

💻
Borja Rodríguez
Borja Rodríguez

🚧
Karrlllis
Karrlllis

📖
jamesmah
jamesmah

💻
Josh Sharpe
Josh Sharpe

🚧
Mantas Šimkūnas
Mantas Šimkūnas

💻 📖
Olivier Overstraete
Olivier Overstraete

🚧
Hemang Kumar
Hemang Kumar

💻 🚧
Luca Ban
Luca Ban

📖
Alex Griffith
Alex Griffith

💻 🚧
bdirito
bdirito

🚧
Ryan
Ryan

🚧
Josué Moreno
Josué Moreno

💻
Marc
Marc

🚧
Florian Gyger
Florian Gyger

💻
jcesarmobile
jcesarmobile

💬 📖 🚧
Rami Khafagi
Rami Khafagi

💻
Bittor Poza
Bittor Poza

💻
Vishal Isharani
Vishal Isharani

🚧 💻
Shunta KARASAWA
Shunta KARASAWA

🚧
Chris Weight
Chris Weight

🚧
Vincent Mahnke
Vincent Mahnke

🚧
tompidom
tompidom

🚇
David Bowdoin
David Bowdoin

💻
tiennp-dev
tiennp-dev

💻
Eljas
Eljas

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

版本与发布记录

@capacitor-community/fcm 共发布 37 个版本,首次发布于 2020/06/28;过去 12 个月发布了 8 个版本。

版本发布日期距今
8.2.2latest2026/09/291 个月前
8.2.12026/09/291 个月前
8.2.02026/09/291 个月前
8.1.02025/12/2610 个月前
8.0.12025/12/0511 个月前
8.0.02025/12/0411 个月前

关于 @capacitor-community/fcm 的常见问题

@capacitor-community/fcm 是什么?

Enable Firebase Cloud Messaging features for Capacitor apps

如何安装 @capacitor-community/fcm?

在项目目录运行 npm install @capacitor-community/fcm,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capacitor-community/fcm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capacitor-community/fcm 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capacitor-community/fcm 可以商用吗?

@capacitor-community/fcm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor-community/fcm 的下载量有多少?

根据 npm 官方下载统计,@capacitor-community/fcm 在 2026/10/09 – 2026/11/07 这 30 天内被下载 134,863 次,日均约 4,495 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capacitor-community/fcm 还在维护吗?

最新版本 8.2.2 发布于 2026/09/29(1 个月前);至今共发布 37 个版本,其中过去 12 个月发布了 8 个;首次发布于 2020/06/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。