快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-firebase-messaging`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/firebase-messaging/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-firebase-messaging` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-firebase-messagingnpx cap sync导入
“导入”章节import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';快速入门
章节标题为“快速入门”import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();console.log(result);示例:requestPermissions
标题为“示例:requestPermissions”的部分import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.requestPermissions();console.log(result);示例:isSupported
章节标题:“示例:isSupported”import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.isSupported();console.log(result);常见坑点
章节标题为“常见陷阱”- 权限: 调用
checkPermissions()然后requestPermissions()在需要时。 - 监听器: 在页面卸载时移除句柄。
- 唯一可信来源:信任
src/definitions.ts在 GitHub 仓库中。
API 概览
章节标题:“API 概览”checkPermissions
“checkPermissions” 章节检查接收推送通知的权限。
当 Android,此方法仅需在 Android 13+ 上调用。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();console.log(result);requestPermissions
“requestPermissions” 章节请求接收推送通知的权限。
当 Android,此方法仅需在 Android 13+ 上调用。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.requestPermissions();console.log(result);isSupported
标题为“isSupported”的部分检查所有必需的 API 是否存在。
始终返回 true 在 Android 和 iOS 上。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.isSupported();console.log(result);getToken
标题为“getToken”的章节注册应用以接收推送通知。 返回一个可用于向该 Messaging 实例发送推送消息的 FCM token。
此方法还会重新启用 FCM 自动初始化。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getToken();// The result holds sensitive values: use it without logging it.deleteToken
标题为 “deleteToken” 的章节删除 FCM token 并注销应用,以停止接收推送通知。 例如,可以在用户退出登录时调用。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteToken();getDeliveredNotifications
标题为 “getDeliveredNotifications” 的章节获取通知栏中可见的通知列表。
注意:这将返回所有已送达的通知(包括本地通知),而不仅仅是 FCM 通知。
在 Android 上,FCM 通知中的 data 字段将不会被包含在内。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getDeliveredNotifications();console.log(result);removeDeliveredNotifications
标题为 “removeDeliveredNotifications” 的章节从通知栏中移除指定通知。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeDeliveredNotifications({ notifications: [] });removeAllDeliveredNotifications
“removeAllDeliveredNotifications” 章节从通知栏中移除所有通知。
注意:此操作将移除所有已送达的通知(包括本地通知),而不仅仅是 FCM 通知。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeAllDeliveredNotifications();subscribeToTopic
“subscribeToTopic” 章节在后台订阅主题。
仅适用于 Android 和 iOS。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.subscribeToTopic({ topic: 'topic' });unsubscribeFromTopic
“unsubscribeFromTopic” 章节在后台取消订阅主题。
仅适用于 Android 和 iOS。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.unsubscribeFromTopic({ topic: 'topic' });createChannel
“createChannel” 章节创建一个通知渠道。
仅适用于 Android (SDK 26+)。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.createChannel({ id: 'id-123', name: 'example',});deleteChannel
“deleteChannel” 章节删除一个通知渠道。
仅适用于 Android (SDK 26+)。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteChannel({ id: 'id-123' });listChannels
“listChannels” 章节列出所有可用的通知渠道。
仅适用于 Android (SDK 26+)。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.listChannels();console.log(result);类型参考
“类型参考” 章节PermissionStatus
标题为“PermissionStatus”的章节export interface PermissionStatus { /** * @since 0.2.2 */ receive: PermissionState;}IsSupportedResult
“IsSupportedResult” 章节export interface IsSupportedResult { /** * @since 0.3.1 */ isSupported: boolean;}GetTokenOptions
“GetTokenOptions” 章节export interface GetTokenOptions { /** * Your VAPID public key, which is required to retrieve the current registration token on the web. * * Only available for Web. */ vapidKey?: string; /** * The service worker registration for receiving push messaging. * If the registration is not provided explicitly, you need to have a `firebase-messaging-sw.js` at your root location. * * Only available for Web. */ serviceWorkerRegistration?: ServiceWorkerRegistration;}GetTokenResult
“GetTokenResult” 章节export interface GetTokenResult { /** * @since 0.2.2 */ token: string;}GetDeliveredNotificationsResult
“GetDeliveredNotificationsResult” 章节export interface GetDeliveredNotificationsResult { /** * @since 0.2.2 */ notifications: Notification[];}RemoveDeliveredNotificationsOptions
“RemoveDeliveredNotificationsOptions” 章节export interface RemoveDeliveredNotificationsOptions { /** * @since 0.4.0 */ notifications: Notification[];}SubscribeToTopicOptions
“SubscribeToTopicOptions” 章节export interface SubscribeToTopicOptions { /** * The name of the topic to subscribe. * * @since 0.2.2 */ topic: string;}UnsubscribeFromTopicOptions
“UnsubscribeFromTopicOptions” 章节export interface UnsubscribeFromTopicOptions { /** * The name of the topic to unsubscribe from. * * @since 0.2.2 */ topic: string;}CreateChannelOptions
“CreateChannelOptions” 章节export type CreateChannelOptions = Channel;DeleteChannelOptions
“DeleteChannelOptions” 章节export interface DeleteChannelOptions { /** * The channel identifier. * * @since 1.4.0 */ id: string;}ListChannelsResult
“ListChannelsResult” 章节export interface ListChannelsResult { channels: Channel[];}TokenReceivedListener
标题为 “TokenReceivedListener” 的章节用于接收 Token 接收事件的回调函数。
export type TokenReceivedListener = (event: TokenReceivedEvent) => void;单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节, 设备 有关 Devices 的实现细节,以及 Bundle 有关 Bundles 中的实现细节。