开始使用
复制一个包含安装步骤和本插件的完整 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.
安装
标题:安装bun add @capgo/capacitor-firebase-messagingbunx cap sync导入
标题:导入import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';API 概述
标题:API 概述checkPermissions
Section titled “checkPermissions”检查推送通知权限
On Android在 Android 13+ 上调用此方法
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.checkPermissions();requestPermissions
Section titled “requestPermissions”请求推送通知权限
On 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注册应用程序以接收推送通知。 返回一个可用于向该Messaging实例发送推送消息的FCM令牌。 此方法还重新启用了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通知,但不仅仅是FCM通知。
在Android设备上,FCM通知的data字段不会被包含。
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
订阅主题(subscribeToTopic)在后台订阅主题
仅适用于 Android 和 iOS
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.subscribeToTopic({} as SubscribeToTopicOptions);unsubscribeFromTopic
取消订阅主题(unsubscribeFromTopic)在后台取消订阅主题
仅适用于 Android 和 iOS
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.unsubscribeFromTopic({} as UnsubscribeFromTopicOptions);createChannel
创建通知频道(createChannel)创建通知频道
Only available for Android (SDK 26+).
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.createChannel({} as CreateChannelOptions);deleteChannel
__CAPGO_KEEP_0__删除通知频道
仅适用于 Android (SDK 26+)
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteChannel({} as DeleteChannelOptions);listChannels
__CAPGO_KEEP_0__查看可用的通知频道
仅适用于 Android (SDK 26+)
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.listChannels();类型参考
__CAPGO_KEEP_0__PermissionStatus
__CAPGO_KEEP_0__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
获取已送达通知结果export interface GetDeliveredNotificationsResult { /** * @since 0.2.2 */ notifications: Notification[];}RemoveDeliveredNotificationsOptions
移除已送达通知选项export interface RemoveDeliveredNotificationsOptions { /** * @since 0.4.0 */ notifications: Notification[];}SubscribeToTopicOptions
订阅主题选项export interface SubscribeToTopicOptions { /** * The name of the topic to subscribe. * * @since 0.2.2 */ topic: string;}UnsubscribeFromTopicOptions
Section titled “__CAPGO_KEEP_0__”export interface UnsubscribeFromTopicOptions { /** * The name of the topic to unsubscribe from. * * @since 0.2.2 */ topic: string;}CreateChannelOptions
Section titled “__CAPGO_KEEP_0__”export type CreateChannelOptions = Channel;DeleteChannelOptions
Section titled “__CAPGO_KEEP_0__”export interface DeleteChannelOptions { /** * The channel identifier. * * @since 1.4.0 */ id: string;}ListChannelsResult
Section titled “__CAPGO_KEEP_0__”export interface ListChannelsResult { channels: Channel[];}TokenReceivedListener
Section titled “__CAPGO_KEEP_0__”__CAPGO_KEEP_0__ 回调函数,用于接收 token 接收事件。
export type TokenReceivedListener = (event: TokenReceivedEvent) => void;真实数据来源
真实来源本页面由插件生成 src/definitions.ts当公共API在上游发生变化时,请重新同步