# @capacitor-firebase/messaging 中文文档

规范地址：https://npmcn.com/package/%40capacitor-firebase/messaging/

版本：8\.6\.0
npm 元数据更新时间：2026-10-31T07:03:04.907Z
中文译文更新时间：2026-11-09T01:42:50.598Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40capacitor-firebase/messaging
原文对照：https://npmcn.com/package/%40capacitor-firebase/messaging/?lang=original
项目主页：https://capawesome.io/
源码仓库：https://github.com/capawesome-team/capacitor-firebase

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：213 kB，32 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：firebase ^12\.6\.0，@capacitor/core \>=8\.0\.0
- 首次发布：2022-05-04T20:40:41.438Z
- 最新版本发布：2026-10-31T07:03:04.565Z
- 近 30 天下载量：1362463（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：63（近 12 个月 13 个）

## 中文简介

在Android,iOS,和Web上为Firebase Cloud Massaging(FCM)提供电容插件.

## 安装

```sh
npm install @capacitor-firebase/messaging
```

也可以使用：`pnpm add @capacitor-firebase/messaging`、`yarn add @capacitor-firebase/messaging`、`bun add @capacitor-firebase/messaging`

## README

# 电源火库云通讯插件

非官方电容插件 [火地云通讯](https://firebase.google.com/docs/cloud-messaging)\[^1\]

[![Thousands of teams ship faster with Capawesome Cloud Native Builds and Live Updates](https://capawesome.io/assets/banners/cloud-teams-ship-faster-with-capacitor.png)](https://capawesome.io/)

## 使用大小写

Firebase Cloud Message 插件通常用于与用户接触并重新接触,并使用按键通知,例如:

-   **推动通知**:将新内容,消息,或报价通知用户,即使应用程序不在前台.
-   **基于主题的信息**:让用户订阅新闻类等专题,并同时向某一专题的所有订阅者发送信息.
-   **设备瞄准**: 获取设备的FCM令牌,向特定用户或设备发送推取消息.
-   **背景数据同步**: 接收背景中的数据驱动通知以保持应用程序内容的更新.
-   **通知管理**: 列出、删除或清除通知屏幕上可见的通知.

## 兼容性

| 插件版本 | 电容版本 | 状态 |
| --- | --- | --- |
| 8.x.x (英语) | \>=8.x.x | 积极支持 |
| 7.x.x (中文(简体) ) | 7.x.x (中文(简体) ) | 折旧 |
| 6.x.x (中文(简体) ) | 6.x.x (中文(简体) ) | 折旧 |
| 5.x.x (中文(简体) ) | 5.x.x (中文(简体) ) | 折旧 |
| 1.x.x (中文(简体) ) | 4.x.x (中文(简体) ) | 折旧 |

## 指南

-   [电容推进通知指南](https://capawesome.io/blog/capacitor-push-notifications-guide/)
-   [如何在电容App中使用火把](https://capawesome.io/blog/how-to-use-firebase-in-a-capacitor-app/):从推动通知到分析与崩溃报告——一个指南中的每个Firebase插件.

## 安装

你可以用我们的 **AI 辅助设置** 以安装插件. 添加数据 [超级技能](https://github.com/capawesome-team/skills) 至您的 AI 工具,使用以下命令:

```bash
npx skills add capawesome-team/skills --skill capacitor-plugins
```

然后使用下列提示:

```
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capacitor-firebase/messaging` plugin in my project.
```

如果你喜欢 **手动设置**,通过运行以下命令来安装插件,并遵循下面的平台特定指令:

```bash
npm install @capacitor-firebase/messaging firebase
npx cap sync
```

如果您还没有将 Firebase 添加到您的项目中([机器人](https://github.com/capawesome-team/capacitor-firebase/blob/main/docs/firebase-setup.md#android) 编号 : [iOS系统](https://github.com/capawesome-team/capacitor-firebase/blob/main/docs/firebase-setup.md#ios) 编号 : [网页](https://github.com/capawesome-team/capacitor-firebase/blob/main/docs/firebase-setup.md#web)) (中文(简体) ).

### 机器人

#### 变量

如果需要, 您可以在您的应用程序中定义以下工程变量 `variables.gradle` 要更改依赖的默认版本的文件 :

-   `$firebaseMessagingVersion` 版本 `com.google.firebase:firebase-messaging` (默认 : `25.0.1`(中文(简体) )

如果您在工程中遇到其它插件的依赖性冲突, 这会有所帮助 .

#### 按下通知图标

带有相应名称的 Push 通知图标应添加到 `android/app/src/main/AndroidManifest.xml` 文件 :

```xml
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/push_icon_name" />
```

如果没有指定图标,Android会使用应用程序图标,但推出图标应该在透明背景上为白色像素. 由于应用程序图标通常不像这样外观,它显示一个白色正方形或圆形. 因此,建议为推进通知提供一个单独的图标.

#### 防止自动初始化

当生成注册符后,库会将标识符和配置数据上传至Firebase. 如果您更喜欢防止符号自动生成, 请通过将这些元数据值添加到 `android/app/src/main/AndroidManifest.xml` 文件 :

```xml
<meta-data
    android:name="firebase_messaging_auto_init_enabled"
    android:value="false" />
<meta-data
    android:name="firebase_analytics_collection_enabled"
    android:value="false" />
```

### iOS系统

#### Swift 软件包管理器

在您的 `capacitor.config.json` (或 `capacitor.config.ts`) 避免 [SwiftPM 软件包识别相撞](https://github.com/capawesome-team/capacitor-firebase/issues/959)数字 :

```json
{
  "experimental": {
    "ios": {
      "spm": {
        "packageOptions": {
          "@capacitor-firebase/messaging": {
            "symlink": true
          }
        }
      }
    }
  }
}
```

**请注意**目标 `packageOptions` 支持需要控制器 CLI **8.4.0+ (中文(简体) )**。 。 。 .

#### 设置

> **重要**: 确保没有安装其他 Capacitor Push 通知插件(参见 [这里](https://github.com/capawesome-team/capacitor-firebase/pull/267#issuecomment-1328885820)) (中文(简体) ).

见 [先决条件](https://capacitorjs.com/docs/guides/push-notifications-firebase#prerequisites) 并首先完成先决条件.

见 [上传 APNS 证书或密钥到 Firebase](https://capacitorjs.com/docs/guides/push-notifications-firebase#upload-the-apns-certificate-or-key-to-firebase) 并遵循将 APNS 证书或 APNS Auth 密钥上传至 Firebase 的指令.

> 如果您对指令有困难,您也可以查看其中的相应章节 [本指南](https://capawesome.io/blog/capacitor-push-notifications-guide/#ios)。 。 。 .

在您的应用程序中添加以下内容 `AppDelegate.swift`数字 :

```swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    NotificationCenter.default.post(name: Notification.Name.init("didReceiveRemoteNotification"), object: completionHandler, userInfo: userInfo)
}
```

**请注意**: 如果您结合使用此插件 `@capacitor-firebase/authentication`,然后在应用程序中添加以下内容: `AppDelegate.swift`数字 :

```diff
+ import FirebaseAuth

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
+    if Auth.auth().canHandle(url) {
+      return true
+    }
    return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
}
```

#### 防止自动初始化

当生成注册符后,库会将标识符和配置数据上传至Firebase. 如果您喜欢防止符号自动生成, 请编辑您的 FCM 自动初始化 `ios/App/App/Info.plist` 设置 `FirebaseMessagingAutoInitEnabled` 键到 `NO`。 。 。 .

### 网页

1.  见 [用 FCM 配置网络证书](https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_with) 并遵守正确配置您网络证书的指令.
2.  添加一个 `firebase-messaging-sw.js` 文件到域名的根。 如果您不想在背景中收到按键通知, 此文件可能为空 . 见 [在服务人员中设定通知选项](https://firebase.google.com/docs/cloud-messaging/js/receive#setting_notification_options_in_the_service_worker) 更多信息.

## 配置

在 iOS 上, 您可以配置当应用程序处于前景时, 如何显示按键通知 .

| Prop | 类型 | 说明 | 默认 | 自兹 |
| --- | --- | --- | --- | --- |
| **`presentationOptions`** | `PresentationOption[]` | 这是一组可以组合的字符串。 数组中可能的值是: - `badge`: 应用程序图标上的徽章数被更新(默认值) - `sound`: 设备在收到推力通知时会响起/振动 -- `alert`:按下通知作为标语显示,并在通知清单中显示 -- `criticalAlert`: 驱动通知在本地对话框中显示并绕过哑声开关 如果不需要任何选项,则可以提供一个空阵列。 仅供iOS使用. | `["alert", "badge", "sound"]` | 0.2.2 (中文(简体) ) |

### 实例

单位 `capacitor.config.json`数字 :

```json
{
  "plugins": {
    "FirebaseMessaging": {
      "presentationOptions": ["alert", "badge", "sound"]
    }
  }
}
```

单位 `capacitor.config.ts`数字 :

```ts
/// <reference types="@capacitor-firebase/messaging" />

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  plugins: {
    FirebaseMessaging: {
      presentationOptions: ["alert", "badge", "sound"],
    },
  },
};

export default config;
```

## 演示

这里有一个实例: [robingenz/电容器-火力基地-插座-demo](https://github.com/robingenz/capacitor-firebase-plugin-demo)

## 启动模板

以下启动模板可供使用:

-   [角起火点](https://ionstarter.dev/)

## 使用量

以下例子说明如何检查和请求通知权限、管理FCM令牌、倾听收到的通知及其行动、订阅专题和管理已发送的通知.

### 检查和请求权限

检查并请求允许接收推出通知 。 在Android上,这些方法只需要调用Android 13+:

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const checkPermissions = async () => {
  const result = await FirebaseMessaging.checkPermissions();
  return result.receive;
};

const requestPermissions = async () => {
  const result = await FirebaseMessaging.requestPermissions();
  return result.receive;
};
```

### 获取或删除 FCM 令牌

注册应用程序以接收推取通知并取回可用于向设备发送推取消息的 FCM 令牌. 删除指使符,例如当一个用户签名退出时,以停止接收按键通知. 当您收到使用 `tokenReceived` 事件,仅可在Android和iOS上获取:

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const getToken = async () => {
  const result = await FirebaseMessaging.getToken();
  return result.token;
};

const deleteToken = async () => {
  await FirebaseMessaging.deleteToken();
};

const addTokenReceivedListener = async () => {
  await FirebaseMessaging.addListener('tokenReceived', event => {
    console.log('tokenReceived', { event });
  });
};
```

### 收听收到的按键通知

收到催促通知后予以通知。 如果应用程序在背景中,这个收听器只用于Android上的数据推取通知和iOS上无声推取通知:

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const addNotificationReceivedListener = async () => {
  await FirebaseMessaging.addListener('notificationReceived', event => {
    console.log('notificationReceived', { event });
  });
};
```

### 对通知行动的反应

在按推器通知采取行动时,例如用户点击时,应通知。 仅在Android和iOS上提供:

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const addNotificationActionPerformedListener = async () => {
  await FirebaseMessaging.addListener('notificationActionPerformed', event => {
    console.log('notificationActionPerformed', { event });
  });
};
```

### 订阅主题

订阅或不订阅一个主题,以接收发送到该主题的按键信息。 仅在Android和iOS上提供:

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const subscribeToTopic = async () => {
  await FirebaseMessaging.subscribeToTopic({ topic: 'news' });
};

const unsubscribeFromTopic = async () => {
  await FirebaseMessaging.unsubscribeFromTopic({ topic: 'news' });
};
```

### 管理已送达的通知

获取、删除或清除目前在通知屏幕上可见的通知 :

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const getDeliveredNotifications = async () => {
  const result = await FirebaseMessaging.getDeliveredNotifications();
  return result.notifications;
};

const removeDeliveredNotifications = async () => {
  await FirebaseMessaging.removeDeliveredNotifications({
    notifications: [
      {
        id: '798dfhliblqew89pzads',
      },
    ],
  });
};

const removeAllDeliveredNotifications = async () => {
  await FirebaseMessaging.removeAllDeliveredNotifications();
};
```

### 删除所有听众

删除所有为此插件添加的听众 :

```typescript
import { FirebaseMessaging } from '@capacitor-firebase/messaging';

const removeAllListeners = async () => {
  await FirebaseMessaging.removeAllListeners();
};
```

## API 密码

-   [`checkPermissions()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-checkpermissions)
-   [`requestPermissions()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-requestpermissions)
-   [`isSupported()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-issupported)
-   [`getToken(...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-gettoken)
-   [`deleteToken()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-deletetoken)
-   [`getDeliveredNotifications()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-getdeliverednotifications)
-   [`removeDeliveredNotifications(...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-removedeliverednotifications)
-   [`removeAllDeliveredNotifications()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-removealldeliverednotifications)
-   [`subscribeToTopic(...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-subscribetotopic)
-   [`unsubscribeFromTopic(...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-unsubscribefromtopic)
-   [`createChannel(...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-createchannel)
-   [`deleteChannel(...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-deletechannel)
-   [`listChannels()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-listchannels)
-   [`addListener('tokenReceived', ...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-addlistenertokenreceived-)
-   [`addListener('notificationReceived', ...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-addlistenernotificationreceived-)
-   [`addListener('notificationActionPerformed', ...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-addlistenernotificationactionperformed-)
-   [`addListener('apnsTokenReceived', ...)`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-addlistenerapnstokenreceived-)
-   [`removeAllListeners()`](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-removealllisteners)
-   [接口](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-interfaces)
-   [类型异名](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-type-aliases)
-   [数字](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-enums)

### 检查( )

```typescript
checkPermissions() => Promise<PermissionStatus>
```

请检查date=中的日期值 (帮助) .

打开 **机器人**,这种方法只需要调用Android 13+.

**回返情况:** `Promise<[PermissionStatus](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-permissionstatus)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 请求( )

```typescript
requestPermissions() => Promise<PermissionStatus>
```

请求允许接收按压通知 .

打开 **机器人**,这种方法只需要调用Android 13+.

**回返情况:** `Promise<[PermissionStatus](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-permissionstatus)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 已支持 ()

```typescript
isSupported() => Promise<IsSupportedResult>
```

检查所有需要的API是否存在 .

总是返回 `true` (原始内容存档于2019-10-12). Android and iOS.

**回返情况:** `Promise<[IsSupportedResult](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-issupportedresult)>`

**自:** 0.3.1 (中文(简体) )

* * *

### 获取托肯(...)

```typescript
getToken(options?: GetTokenOptions | undefined) => Promise<GetTokenResult>
```

注册应用程序以接收按下通知 . 返回一个 FCM 令牌, 该令牌可用于向 Message 实例发送按键消息 .

这种方法还可以重新使用FCM自動活化.

| 参数 | 类型 |
| --- | --- |
| **`options`** | `[GetTokenOptions](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-gettokenoptions)` |

**回返情况:** `Promise<[GetTokenResult](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-gettokenresult)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 删除 Token ()

```typescript
deleteToken() => Promise<void>
```

删除 FCM 令牌并取消该应用程序的注册, 以停止接收按键通知 . 例如,当用户签名退出时,可以调用.

**自:** 0.2.2 (中文(简体) )

* * *

### 获得交付通知( )

```typescript
getDeliveredNotifications() => Promise<GetDeliveredNotificationsResult>
```

获取通知屏幕上可见的通知列表.

说明: 这将退回所有已送达的通知,包括当地通知,而不仅仅是联邦管理通知.

关于Android,FCM通知的数据字段将不包含.

**回返情况:** `Promise<[GetDeliveredNotificationsResult](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-getdeliverednotificationsresult)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 删除已交付通知(...)

```typescript
removeDeliveredNotifications(options: RemoveDeliveredNotificationsOptions) => Promise<void>
```

从通知屏幕上删除特定通知 .

| 参数 | 类型 |
| --- | --- |
| **`options`** | `[RemoveDeliveredNotificationsOptions](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-removedeliverednotificationsoptions)` |

**自:** 0.2.2 (中文(简体) )

* * *

### 删除所有已交付通知( )

```typescript
removeAllDeliveredNotifications() => Promise<void>
```

从通知屏幕上删除所有通知 .

说明: 这将删除所有已发送的通知,包括当地通知,而不只是联邦管理通知.

**自:** 0.2.2 (中文(简体) )

* * *

### 订阅TOTOPIC(...)

```typescript
subscribeToTopic(options: SubscribeToTopicOptions) => Promise<void>
```

订阅背景主题 .

仅供Android和iOS使用.

| 参数 | 类型 |
| --- | --- |
| **`options`** | `[SubscribeToTopicOptions](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-subscribetotopicoptions)` |

**自:** 0.2.2 (中文(简体) )

* * *

### 从Topic(...) 取消订阅

```typescript
unsubscribeFromTopic(options: UnsubscribeFromTopicOptions) => Promise<void>
```

在背景中不订阅主题 .

仅供Android和iOS使用.

| 参数 | 类型 |
| --- | --- |
| **`options`** | `[UnsubscribeFromTopicOptions](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-unsubscribefromtopicoptions)` |

**自:** 0.2.2 (中文(简体) )

* * *

### 创建频道(...)

```typescript
createChannel(options: CreateChannelOptions) => Promise<void>
```

创建通知通道 .

仅供Android(SDK 26+)使用.

| 参数 | 类型 |
| --- | --- |
| **`options`** | `[Channel](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-channel)` |

**自:** 1.4.0 (中文(简体) )

* * *

### 删除频道(...)

```typescript
deleteChannel(options: DeleteChannelOptions) => Promise<void>
```

删除通知频道.

仅供Android(SDK 26+)使用.

| 参数 | 类型 |
| --- | --- |
| **`options`** | `[DeleteChannelOptions](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-deletechanneloptions)` |

**自:** 1.4.0 (中文(简体) )

* * *

### 列表频道( )

```typescript
listChannels() => Promise<ListChannelsResult>
```

列出可用的通知渠道.

仅供Android(SDK 26+)使用.

**回返情况:** `Promise<[ListChannelsResult](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-listchannelsresult)>`

**自:** 1.4.0 (中文(简体) )

* * *

### 添加收听器( 'token reading',...)

```typescript
addListener(eventName: 'tokenReceived', listenerFunc: TokenReceivedListener) => Promise<PluginListenerHandle>
```

收到新的 FCM 令牌后调用 .

仅供Android和iOS使用.

| 参数 | 类型 |
| --- | --- |
| **`eventName`** | `'tokenReceived'` |
| **`listenerFunc`** | `[TokenReceivedListener](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-tokenreceivedlistener)` |

**回返情况:** `Promise<[PluginListenerHandle](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-pluginlistenerhandle)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 添加倾听器( 通知已收到,...)

```typescript
addListener(eventName: 'notificationReceived', listenerFunc: NotificationReceivedListener) => Promise<PluginListenerHandle>
```

接到新的催促通知后打电话.

打开 **机器人**如果应用程序在 _前景_。 。 。 . 如果应用程序在 _背景情况_,那么这个收听器只被调用到数据驱动通知上. 见 [https://firebase.google.com/docs/cloud-message/android/receive# 处理 messages (英语)](https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages) 更多信息.

打开 **iOS系统**如果应用程序在 _前景_。 。 。 . 如果应用程序在 _背景情况_,那么这个收听器只被叫作无声的按键通知(与 `content-available` ) (中文(简体) ). 见 [https://developer.apple.com/library/archive/documentation/Networking Internet/Conceptual/Remote NotificationsPG/PayloadKeyReference.html 互联网档案馆的存檔,存档日期2013-12-02](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html) 更多信息.

| 参数 | 类型 |
| --- | --- |
| **`eventName`** | `'notificationReceived'` |
| **`listenerFunc`** | `[NotificationReceivedListener](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-notificationreceivedlistener)` |

**回返情况:** `Promise<[PluginListenerHandle](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-pluginlistenerhandle)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 添加倾听器( “ 通知动作已执行 ” ,...)

```typescript
addListener(eventName: 'notificationActionPerformed', listenerFunc: NotificationActionPerformedListener) => Promise<PluginListenerHandle>
```

执行新的推动通知行动时呼叫.

仅供Android和iOS使用.

| 参数 | 类型 |
| --- | --- |
| **`eventName`** | `'notificationActionPerformed'` |
| **`listenerFunc`** | `[NotificationActionPerformedListener](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-notificationactionperformedlistener)` |

**回返情况:** `Promise<[PluginListenerHandle](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-pluginlistenerhandle)>`

**自:** 0.2.2 (中文(简体) )

* * *

### 添加倾听器( “ apnsTokenreaived ” ,...)

```typescript
addListener(eventName: 'apnsTokenReceived', listenerFunc: ApnsTokenReceivedListener) => Promise<PluginListenerHandle>
```

收到APNs的令牌后调用.

仅供iOS使用.

| 参数 | 类型 |
| --- | --- |
| **`eventName`** | `'apnsTokenReceived'` |
| **`listenerFunc`** | `[ApnsTokenReceivedListener](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-apnstokenreceivedlistener)` |

**回返情况:** `Promise<[PluginListenerHandle](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-pluginlistenerhandle)>`

**自:** 8.2.0 (中文(简体) )

* * *

### 删除所有收听器( )

```typescript
removeAllListeners() => Promise<void>
```

删除此插件的所有听众 .

**自:** 0.2.2 (中文(简体) )

* * *

### 接口

#### 权限状态

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`receive`** | `[PermissionState](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-permissionstate)` | 0.2.2 (中文(简体) ) |

#### 支持的结果

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`isSupported`** | `boolean` | 0.3.1 (中文(简体) ) |

#### 获取托肯结果

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`token`** | `string` | 0.2.2 (中文(简体) ) |

#### 获取托肯选项

| Prop | 类型 | 说明 |
| --- | --- | --- |
| **`vapidKey`** | `string` | 您的 VAPID 公用密钥, 用于在网络上获取当前注册符 。 仅供Web使用. |
| **`serviceWorkerRegistration`** | `ServiceWorkerRegistration` | 服务员登记接收推送信息. 若未明确提供登记,则需提供 `firebase-messaging-sw.js` 在你的根部位置。 仅供Web使用. |

#### 获取交付通知

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`notifications`** | `Notification[]` | 0.2.2 (中文(简体) ) |

#### 通知

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`body`** | `string` | 通知有效载荷. | 0.2.2 (中文(简体) ) |
| **`clickAction`** | `string` | 对打开通知的用户采取的行动。 仅供Android使用. | 0.2.2 (中文(简体) ) |
| **`data`** | `unknown` | 列入推进通知有效载荷的任何其他数据. | 0.2.2 (中文(简体) ) |
| **`id`** | `string` | 通知识别符. | 0.2.2 (中文(简体) ) |
| **`image`** | `string` | 在设备上下载并在通知中显示的图像的 URL。 仅供Web使用. | 0.2.2 (中文(简体) ) |
| **`link`** | `string` | 与通知书的深度链接. 仅供Android使用. | 0.2.2 (中文(简体) ) |
| **`subtitle`** | `string` | 通知副标题。 仅供iOS使用. | 0.2.2 (中文(简体) ) |
| **`tag`** | `string` | 通知字符串标识符 。 仅供Android使用. | 0.4.0 (中文(简体) ) |
| **`title`** | `string` | 通知标题. | 0.2.2 (中文(简体) ) |

#### 删除已交付通知选项

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`notifications`** | `Notification[]` | 0.4.0 (中文(简体) ) |

#### 订阅选项

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`topic`** | `string` | 要订阅的主题名称 . | 0.2.2 (中文(简体) ) |

#### 取消从选项订阅

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`topic`** | `string` | 未订阅的主题名称 . | 0.2.2 (中文(简体) ) |

#### 频道

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`description`** | `string` | 本频道的描述(向用户介绍). | 1.4.0 (中文(简体) ) |
| **`id`** | `string` | 频道标识符. | 1.4.0 (中文(简体) ) |
| **`importance`** | `[Importance](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-importance)` | 发送到此频道的通知的中断级别 . | 1.4.0 (中文(简体) ) |
| **`lightColor`** | `string` | 发送到此频道的通知的浅色 。 只有在该通道上启用了灯光,设备也支持时才支持. 支持的颜色格式为 `#RRGGBB` 财务报告和审定财务报表 `#RRGGBBAA`。 。 。 . | 1.4.0 (中文(简体) ) |
| **`lights`** | `boolean` | 发送到该频道的通知是否应该显示该频道所支持的设备的通知灯. | 1.4.0 (中文(简体) ) |
| **`name`** | `string` | 此频道的名称( 向用户介绍) . | 1.4.0 (中文(简体) ) |
| **`sound`** | `string` | 发送到该频道的通知所应播放的声音 。 至少重要的通知渠道 `3` 应该有一个声音。 声音文件的文件名称应相对于 Android app 指定 `res/raw` 目录. | 1.4.0 (中文(简体) ) |
| **`vibration`** | `boolean` | 发送到此频道的通知是否应该振动 . | 1.4.0 (中文(简体) ) |
| **`visibility`** | `[Visibility](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-visibility)` | 该频道张贴的通知的能见度。 此设置用于显示发送到此频道的通知是否出现在锁定屏幕上, 如果有, 是否以编辑后的形式出现 . | 1.4.0 (中文(简体) ) |

#### 删除频道选项

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`id`** | `string` | 频道标识符. | 1.4.0 (中文(简体) ) |

#### ListChannels 结果

| Prop | 类型 |
| --- | --- |
| **`channels`** | `Channel[]` |

#### 插件听筒

| Prop | 类型 |
| --- | --- |
| **`remove`** | `() => Promise<void>` |

#### 收件活动

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`token`** | `string` | 0.2.2 (中文(简体) ) |

#### 通知 收到活动

| Prop | 类型 | 自兹 |
| --- | --- | --- |
| **`notification`** | `[Notification](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-notification)` | 0.2.2 (中文(简体) ) |

#### 通知动作已执行

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`actionId`** | `string` | 根据通知采取的行动. | 0.2.2 (中文(简体) ) |
| **`inputValue`** | `string` | 关于通知行动的案文已输入。 仅供iOS使用. | 0.2.2 (中文(简体) ) |
| **`notification`** | `[Notification](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-notification)` | 采取行动的通知. | 0.2.2 (中文(简体) ) |

#### Apns 接收事件

| Prop | 类型 | 说明 | 自兹 |
| --- | --- | --- | --- |
| **`token`** | `string` | 本地 APNs 符号作为大写十六进制编码字符串 . | 8.2.0 (中文(简体) ) |

### 类型异名

#### 权限状态

`'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'`

#### 创建频道选项

`[Channel](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-channel)`

#### 已接收 听众

召回以接收收到的指使事件 .

`(event: [TokenReceivedEvent](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-tokenreceivedevent)): void`

#### 通知 实收 听众

召回收到通知的事件.

`(event: [NotificationReceivedEvent](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-notificationreceivedevent)): void`

#### 通知动作已执行

召回接收已执行的通知动作事件 .

`(event: [NotificationActionPerformedEvent](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-notificationactionperformedevent)): void`

#### 已接收 听众

召回接收APNs所收到事件.

`(event: [ApnsTokenReceivedEvent](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-apnstokenreceivedevent)): void`

### 数字

#### 重要性

| 成员 | 数值 | 自兹 |
| --- | --- | --- |
| **`Min`** | `1` | 1.4.0 (中文(简体) ) |
| **`Low`** | `2` | 1.4.0 (中文(简体) ) |
| **`Default`** | `3` | 1.4.0 (中文(简体) ) |
| **`High`** | `4` | 1.4.0 (中文(简体) ) |
| **`Max`** | `5` | 1.4.0 (中文(简体) ) |

#### 可见度

| 成员 | 数值 | 自兹 |
| --- | --- | --- |
| **`Secret`** | `-1` | 1.4.0 (中文(简体) ) |
| **`Private`** | `0` | 1.4.0 (中文(简体) ) |
| **`Public`** | `1` | 1.4.0 (中文(简体) ) |

## 财务问题

### Capacitor Firebase Cloud 消息插件和Capacitor Push通知插件有什么区别??

这个 [推进通知](https://capacitorjs.com/docs/apis/push-notifications) 插件支持Android(FCM)和iOS(APNs). 另一方面,Capacitor Firebase Cloud 消息插件则使用 Firebase SDK 用于 [机器人](https://firebase.google.com/docs/cloud-messaging/android/client), (中文(简体) ), [iOS系统](https://firebase.google.com/docs/cloud-messaging/ios/client) 财务报告和审定财务报表 [网页](https://firebase.google.com/docs/cloud-messaging/js/client) 并提供其他功能,如主题订阅和在前场接收通知的能力.

### 为什么 `notificationReceived` 应用程序在后台时不打电话吗?

这是基础平台的预期行为. 在Android上,仅当应用程序处于前台时,就要求收听者发出每次推取通知. 如果该应用程序处于后台,则仅用于数据推取通知. 在iOS上,听众仅在背景中被调用为无声的按键通知,这些是带有: `content-available` 键.

### 为什么我在Android上的推力通知图标是白方块?

Android预计推取通知图标由透明背景上白色像素组成. 如果未指定专用图标,Android会倒回应用程序图标,通常不符合这一要求,因此被显示为白色正方形或圆形. 按 [安装](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-installation) 节.

### 我在收到推车通知前需要请求许可吗?

是的,用那个 `checkPermissions()` 财务报告和审定财务报表 `requestPermissions()` 检查和请求允许接收按键通知的方法,如 [使用示例](https://npmcn.com/package/%40capacitor-firebase/messaging/#readme-check-and-request-permissions) 见上文。 在Android上,这些方法只需要被调用到Android 13+上.

## 相关插件

-   [Firebase 验证](https://capawesome.io/docs/sdks/capacitor/firebase/authentication/):在用户中以 Firebase 认证签名.
-   [火场分析](https://capawesome.io/docs/sdks/capacitor/firebase/analytics/): 记录带有Firebase分析器的事件和用户属性.
-   [徽标](https://capawesome.io/docs/sdks/capacitor/badge/): 访问并更新应用程序图标的徽标编号.

## 通讯

随时更新有关卡帕韦索姆、Capacitor和Ionic生态系统的最新消息和最新消息,因为我们 [《卡帕索克通讯》](https://cloud.capawesome.io/newsletter/)。 。 。 .

## 更改日志

见 [变动情况](https://github.com/capawesome-team/capacitor-firebase/blob/main/packages/messaging/CHANGELOG.md)。 。 。 .

## 许可证

见 [自由主义](https://github.com/capawesome-team/capacitor-firebase/blob/main/packages/messaging/LICENSE)。 。 。 .

\[^1\]: 这个项目不附属于Google LLC或其任何子公司或子公司,也不获得其认可、支持、赞助或批准.

## 常见问题

### @capacitor-firebase/messaging 是什么？

在Android,iOS,和Web上为Firebase Cloud Massaging\(FCM\)提供电容插件\.

### 如何安装 @capacitor-firebase/messaging？

在项目目录运行 npm install @capacitor-firebase/messaging，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@capacitor/core（\>=8\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @capacitor-firebase/messaging 支持 TypeScript 吗？

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

### @capacitor-firebase/messaging 支持 ESM 还是 CommonJS？

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

### @capacitor-firebase/messaging 可以商用吗？

@capacitor-firebase/messaging 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @capacitor-firebase/messaging 的下载量有多少？

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

### @capacitor-firebase/messaging 还在维护吗？

最新版本 8\.6\.0 发布于 2026/10/31（9 天前）；至今共发布 63 个版本，其中过去 12 个月发布了 13 个；首次发布于 2022/05/05。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

