跳转到正文

快速入门

GitHub

你可以使用我们的 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-messaging
npx cap sync
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();
console.log(result);
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.requestPermissions();
console.log(result);
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.isSupported();
console.log(result);
  • 权限: 调用 checkPermissions() 然后 requestPermissions() 在需要时。
  • 监听器: 在页面卸载时移除句柄。
  • 唯一可信来源:信任 src/definitions.ts 在 GitHub 仓库中。

检查接收推送通知的权限。

当 Android,此方法仅需在 Android 13+ 上调用。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();
console.log(result);

请求接收推送通知的权限。

当 Android,此方法仅需在 Android 13+ 上调用。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.requestPermissions();
console.log(result);

检查所有必需的 API 是否存在。

始终返回 true 在 Android 和 iOS 上。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.isSupported();
console.log(result);

注册应用以接收推送通知。 返回一个可用于向该 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.

删除 FCM token 并注销应用,以停止接收推送通知。 例如,可以在用户退出登录时调用。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteToken();

获取通知栏中可见的通知列表。

注意:这将返回所有已送达的通知(包括本地通知),而不仅仅是 FCM 通知。

在 Android 上,FCM 通知中的 data 字段将不会被包含在内。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getDeliveredNotifications();
console.log(result);

从通知栏中移除指定通知。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeDeliveredNotifications({ notifications: [] });

removeAllDeliveredNotifications

“removeAllDeliveredNotifications” 章节

从通知栏中移除所有通知。

注意:此操作将移除所有已送达的通知(包括本地通知),而不仅仅是 FCM 通知。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeAllDeliveredNotifications();

在后台订阅主题。

仅适用于 Android 和 iOS。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.subscribeToTopic({ topic: 'topic' });

在后台取消订阅主题。

仅适用于 Android 和 iOS。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.unsubscribeFromTopic({ topic: 'topic' });

创建一个通知渠道。

仅适用于 Android (SDK 26+)。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.createChannel({
id: 'id-123',
name: 'example',
});

删除一个通知渠道。

仅适用于 Android (SDK 26+)。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteChannel({ id: 'id-123' });

列出所有可用的通知渠道。

仅适用于 Android (SDK 26+)。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.listChannels();
console.log(result);
export interface PermissionStatus {
/**
* @since 0.2.2
*/
receive: PermissionState;
}
export interface IsSupportedResult {
/**
* @since 0.3.1
*/
isSupported: boolean;
}
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;
}
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[];
}
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;
}
export type CreateChannelOptions = Channel;
export interface DeleteChannelOptions {
/**
* The channel identifier.
*
* @since 1.4.0
*/
id: string;
}
export interface ListChannelsResult {
channels: Channel[];
}

用于接收 Token 接收事件的回调函数。

export type TokenReceivedListener = (event: TokenReceivedEvent) => void;

本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节, 设备 有关 Devices 的实现细节,以及 Bundle 有关 Bundles 中的实现细节。