开始使用
安装
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.如果您prefer Manual Setup,安装插件并运行以下命令,按照以下平台特定说明进行操作:
bun add @capgo/capacitor-firebase-messagingbunx cap sync导入
导入import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';API概述
API概述checkPermissions
检查权限检查推送通知权限。
开启 Android在Android 13+上调用此方法。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.checkPermissions();requestPermissions
“requestPermissions””,标题请求推送通知的权限。
在 Android上,这个方法只需要在 Android 13+ 时调用。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.requestPermissions();isSupported
“isSupported””,标题检查所有所需的API是否存在。
总是返回 true 在 Android 和 iOS 上。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.isSupported();getToken
“getToken””,标题注册应用程序以接收推送通知。 返回一个 FCM token,可以用来向该 Messaging 实例发送推送消息。
此方法还重新启用 FCM 自动初始化。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.getToken();deleteToken
标题:删除令牌删除 FCM 令牌并注销应用程序以停止接收推送通知。 可以在用户注销时调用。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteToken();getDeliveredNotifications
标题:获取已交付通知获取可在通知屏幕上显示的通知列表。
注意:此方法将返回所有已交付的通知,包括本地通知,而不仅仅是 FCM 通知。
在 Android 上,FCM 通知的数据字段将不会包含。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.getDeliveredNotifications();removeDeliveredNotifications
标题:删除已交付通知从通知屏幕中移除特定的通知。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeDeliveredNotifications({} as RemoveDeliveredNotificationsOptions);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({} as SubscribeToTopicOptions);unsubscribeFromTopic
标题:取消订阅主题在后台取消订阅主题。
仅适用于 Android 和 iOS。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.unsubscribeFromTopic({} as UnsubscribeFromTopicOptions);createChannel
创建通知频道仅适用于 Android(__CAPGO_KEEP_0__ 26+)。
Only available for Android (SDK 26+).
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.createChannel({} as CreateChannelOptions);deleteChannel
仅适用于 Android(__CAPGO_KEEP_0__ 26+)。复制到剪贴板
Only available for Android (SDK 26+).
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteChannel({} as DeleteChannelOptions);listChannels
复制到剪贴板列出可用的通知频道
仅适用于 Android (SDK 26+)。
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.listChannels();类型参考
类型参考PermissionStatus
权限状态export interface PermissionStatus { /** * @since 0.2.2 */ receive: PermissionState;}IsSupportedResult
是否支持结果export interface IsSupportedResult { /** * @since 0.3.1 */ isSupported: boolean;}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
获取令牌结果export interface GetTokenResult { /** * @since 0.2.2 */ token: string;}GetDeliveredNotificationsResult
Section titled “获取已交付通知结果”export interface GetDeliveredNotificationsResult { /** * @since 0.2.2 */ notifications: Notification[];}RemoveDeliveredNotificationsOptions
Section titled “移除已交付通知选项”export interface RemoveDeliveredNotificationsOptions { /** * @since 0.4.0 */ notifications: Notification[];}SubscribeToTopicOptions
Section titled “订阅主题选项”export interface SubscribeToTopicOptions { /** * The name of the topic to subscribe. * * @since 0.2.2 */ topic: string;}UnsubscribeFromTopicOptions
Section titled “取消订阅主题选项”export interface UnsubscribeFromTopicOptions { /** * The name of the topic to unsubscribe from. * * @since 0.2.2 */ topic: string;}CreateChannelOptions
Section titled “创建频道选项”export type CreateChannelOptions = Channel;DeleteChannelOptions
Section titled “删除频道选项”export interface DeleteChannelOptions { /** * The channel identifier. * * @since 1.4.0 */ id: string;}ListChannelsResult
ListChannelsResultexport interface ListChannelsResult { channels: Channel[];}TokenReceivedListener
TokenReceivedListener接收令牌接收事件的回调。
export type TokenReceivedListener = (event: TokenReceivedEvent) => void;真实来源
真实来源本页面是由插件的 src/definitions.ts重新同步公共API时,重新运行上游的同步。
继续从开始
继续从开始如果您正在使用 开始使用 为计划仪表板和API操作,连接它与 API概述 API概述中实现细节的 介绍 介绍中实现细节的 API密钥 API密钥中实现细节的 设备 设备中实现细节的,和 捆绑包 捆绑包中实现细节的。