跳过内容

开始使用

GitHub

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

Terminal window
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-firebase-messaging` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

Terminal window
bun add @capgo/capacitor-firebase-messaging
bunx 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

获取推送通知的权限

请求推送通知的权限

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

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

始终返回

在 Android 和 iOS 上。 true 复制到剪贴板

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

注册应用程序以接收推送通知。 返回一个 FCM token,可以用来向该 Messaging 实例发送推送消息。

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

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

删除 FCM token 并注销应用程序以停止接收推送通知。 可以在用户注销时调用。

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

获取可见于通知屏幕上的通知列表。

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

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

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

移除特定通知从通知屏幕。

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

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({} as SubscribeToTopicOptions);

unsubscribeFromTopic

标题:取消订阅主题

在后台取消订阅主题。

仅适用于 Android 和 iOS。

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

createChannel

创建频道

创建通知频道。

仅适用于 Android (SDK 26+)。

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

deleteChannel

删除频道

删除通知频道。

仅适用于 Android (SDK 26+)。

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

列出可用的通知频道。

仅适用于 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;
}
export interface GetDeliveredNotificationsResult {
/**
* @since 0.2.2
*/
notifications: Notification[];
}

RemoveDeliveredNotificationsOptions

Section titled “移除已交付通知选项”
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;
}
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;
}

ListChannelsResult

ListChannelsResult
export interface ListChannelsResult {
channels: Channel[];
}

TokenReceivedListener

TokenReceivedListener

接收到令牌事件的回调。

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

真实来源

真实来源

本页面是由插件的 src/definitions.ts重新同步公共API时,重新运行上游的同步。

继续从开始

继续从开始

如果您正在使用 开始使用 为计划仪表板和API操作,连接它 API概述 了解API概述的实现细节 介绍 了解介绍的实现细节 API密钥 了解API密钥的实现细节 设备 了解设备的实现细节,并且 了解包的实现细节