跳过内容

开始

GitHub

您可以使用我们的 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手动设置,安装插件并运行以下命令并遵循以下平台特定说明:

终端窗口
bun add @capgo/capacitor-firebase-messaging
bunx cap sync

导入

导入
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';

API Capgo概览

API概述

checkPermissions

导入

检查权限以接收推送通知

开启 Android在Android 13+上调用此方法

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

requestPermissions

请求权限

申请推送通知的权限。

On Android, 这个方法只需要在 Android 13+ 上调用。

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

标题为“isSupported”

检查所有所需的 API 是否存在。 true 始终返回

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

标题为“getToken”

此方法还会重新启用 FCM 自动初始化。

import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getToken();
// The result holds sensitive values: use it without logging it.

deleteToken

删除令牌

删除 FCM 令牌并注销应用以停止接收推送通知。 例如,当用户注销时,可以调用。

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

getDeliveredNotifications

获取已送达通知

注意:此方法将返回所有已送达通知,包括本地通知,而不是仅 FCM 通知。

在 Android 上,FCM 通知的数据字段将不会包含。

删除已送达通知

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通知。

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 (__CAPGO_KEEP_0__ 26+)

仅适用于 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' });

listChannels

创建频道

仅适用于 Android (__CAPGO_KEEP_0__ 26+)

仅适用于 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

是否支持结果
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

取消订阅主题选项
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

TokenReceivedListener

接收令牌事件的回调

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

真实来源

真实来源

此页面是从插件的 src/definitions.ts重新同步公共API时,API发生了变化。

继续从开始

继续从开始

如果您正在使用 开始 为 API 计划和控制台,连接它 API 概述 有关 API 概述的实现细节 简介 有关简介的实现细节 API 密钥 有关 API 密钥的实现细节 设备 为 __CAPGO_KEEP_0__ 的实现细节 包 为 __CAPGO_KEEP_0__ 的实现细节