开始使用
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
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技能添加到您的AI工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
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-messagingbunx cap sync导入
导入部分import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';API概述
API 简介checkPermissions
检查权限检查接收推送通知的权限。
On Android在 Android 13+ 上调用此方法时,只需调用此方法。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();console.log(result);requestPermissions
requestPermissions请求接收推送通知的权限。
On 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注册应用程序以接收推送通知。 返回一个FCM令牌,可以用来向该Messaging实例发送推送消息。
此方法还重新启用了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令牌并注销应用程序以停止接收推送通知。 例如,可以在用户注销时调用此方法。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteToken();getDeliveredNotifications
标题:获取已交付通知获取可见于通知屏幕上的通知列表。
注意:此操作将返回所有已交付的通知,包括本地通知和FCM通知,但不仅限于FCM通知。
在Android上,FCM通知的data字段将不会被包含。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getDeliveredNotifications();console.log(result);removeDeliveredNotifications
标题:移除已交付通知移除通知屏幕上的特定通知。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeDeliveredNotifications({ notifications: [] });removeAllDeliveredNotifications
标题:移除所有已交付通知移除所有通知屏幕上的通知。
注意:此操作将移除所有已交付的通知,包括本地通知和FCM通知,但不仅限于FCM通知。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeAllDeliveredNotifications();subscribeToTopic
订阅主题在后台订阅主题。
仅适用于 Android 和 iOS。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.subscribeToTopic({ topic: 'topic' });unsubscribeFromTopic
取消订阅主题在后台取消订阅主题。
仅适用于 Android 和 iOS。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.unsubscribeFromTopic({ topic: 'topic' });createChannel
创建通知频道仅适用于 Android (26+).
SDK
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.createChannel({ id: 'id-123', name: 'example',});deleteChannel
删除频道删除通知频道。
仅适用于 Android (SDK 26+)。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteChannel({ id: 'id-123' });listChannels
通知频道列表列出可用的通知频道。
仅适用于 Android (SDK 26+)。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.listChannels();console.log(result);类型参考
类型参考PermissionStatus
权限状态export interface PermissionStatus { /** * @since 0.2.2 */ receive: PermissionState;}IsSupportedResult
标题:IsSupportedResultexport interface IsSupportedResult { /** * @since 0.3.1 */ isSupported: boolean;}GetTokenOptions
标题:GetTokenOptionsexport 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
标题:GetTokenResultexport interface GetTokenResult { /** * @since 0.2.2 */ token: string;}GetDeliveredNotificationsResult
标题:GetDeliveredNotificationsResultexport interface GetDeliveredNotificationsResult { /** * @since 0.2.2 */ notifications: Notification[];}RemoveDeliveredNotificationsOptions
标题:RemoveDeliveredNotificationsOptionsexport interface RemoveDeliveredNotificationsOptions { /** * @since 0.4.0 */ notifications: Notification[];}SubscribeToTopicOptions
标题:SubscribeToTopicOptionsexport interface SubscribeToTopicOptions { /** * The name of the topic to subscribe. * * @since 0.2.2 */ topic: string;}UnsubscribeFromTopicOptions
标题:取消订阅主题选项export interface UnsubscribeFromTopicOptions { /** * The name of the topic to unsubscribe from. * * @since 0.2.2 */ topic: string;}CreateChannelOptions
标题:创建频道选项export type CreateChannelOptions = Channel;DeleteChannelOptions
标题:删除频道选项export interface DeleteChannelOptions { /** * The channel identifier. * * @since 1.4.0 */ id: string;}ListChannelsResult
标题:列出频道结果export interface ListChannelsResult { channels: Channel[];}TokenReceivedListener
标题:接收令牌事件监听器接收令牌事件的回调函数
export type TokenReceivedListener = (event: TokenReceivedEvent) => void;真实来源
名为“真实来源”的部分本页面是根据插件的 src/definitions.ts当公共 API 在上游发生变化时,请重新运行同步。
继续从 Getting Started
名为“继续从 Getting Started”的部分如果您正在使用 开始使用 来规划仪表板和 API 操作,请将其与 API 简介 连接以获取在 API Overview 中的实现细节 介绍 为《介绍》中实现细节的说明 API 键 为《API Keys》中实现细节的说明 设备 context Bundles 为《设备》中实现细节的说明,