Panduan Memulai
Salin prompt setup yang berisi langkah install dan panduan markdown lengkap untuk plugin ini.
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.
Anda bisa menggunakan AI-Assisted Setup kami untuk menginstal plugin ini. Tambahkan skill Capgo ke tool AI Anda dengan command berikut:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-pluginsLalu gunakan prompt berikut:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-firebase-messaging` plugin in my project.Jika Anda lebih memilih Manual Setup, instal plugin dengan menjalankan perintah berikut dan ikuti petunjuk khusus platform di bawah ini:
bun add @capgo/capacitor-firebase-messagingnpx cap syncimport { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';Mulai cepat
Bagian berjudul āMulai cepatāimport { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();console.log(result);Contoh: requestPermissions
Bagian berjudul āContoh: requestPermissionsāimport { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.requestPermissions();console.log(result);Contoh: isSupported
Bagian berjudul āExample: isSupportedāimport { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.isSupported();console.log(result);Kesalahan umum
Bagian berjudul āKesalahan umumā- Izin: panggil
checkPermissions()kemudianrequestPermissions()saat dibutuhkan. - Listeners: hapus handle saat screen unmount.
- Sumber kebenaran tunggal: percayai
src/definitions.tsdi repo GitHub.
Ringkasan API
Bagian berjudul āIkhtisar APIācheckPermissions
Bagian berjudul ācheckPermissionsāPeriksa izin untuk menerima push notification.
Di Android, metode ini hanya perlu dipanggil pada Android 13+.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.checkPermissions();console.log(result);requestPermissions
Bagian berjudul ārequestPermissionsāMinta izin untuk menerima push notification.
Di Android, metode ini hanya perlu dipanggil pada Android 13+.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.requestPermissions();console.log(result);isSupported
Bagian berjudul āisSupportedāChecks if all required APIs exist.
Selalu mengembalikan true pada Android dan iOS.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.isSupported();console.log(result);getToken
Bagian berjudul āgetTokenāDaftarkan aplikasi untuk menerima push notification. Mengembalikan token FCM yang dapat digunakan untuk mengirim pesan push ke instance Messaging tersebut.
Metode ini juga mengaktifkan kembali auto-init FCM.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getToken();// The result holds sensitive values: use it without logging it.deleteToken
Bagian berjudul ādeleteTokenāHapus token FCM dan unregister aplikasi agar berhenti menerima notifikasi push. Bisa dipanggil, misalnya, saat pengguna logout.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteToken();getDeliveredNotifications
Bagian berjudul āgetDeliveredNotificationsāDapatkan daftar notifikasi yang muncul di layar notifikasi.
Catatan: Ini akan mengembalikan semua notifikasi terkirim, termasuk notifikasi lokal, bukan hanya notifikasi FCM.
Di Android, field data dari notifikasi FCM TIDAK akan disertakan.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.getDeliveredNotifications();console.log(result);removeDeliveredNotifications
Bagian berjudul āremoveDeliveredNotificationsāHapus notifikasi tertentu dari layar notifikasi.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeDeliveredNotifications({ notifications: [] });removeAllDeliveredNotifications
Bagian berjudul āremoveAllDeliveredNotificationsāHapus semua notifikasi dari layar notifikasi.
Catatan: Ini akan menghapus semua notifikasi yang masuk, termasuk notifikasi lokal, bukan hanya notifikasi FCM.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.removeAllDeliveredNotifications();subscribeToTopic
Bagian berjudul āsubscribeToTopicāBerlangganan ke topik di background.
Hanya tersedia untuk Android dan iOS.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.subscribeToTopic({ topic: 'topic' });unsubscribeFromTopic
Bagian berjudul āunsubscribeFromTopicāBerhenti berlangganan dari topik di background.
Hanya tersedia untuk Android dan iOS.
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.unsubscribeFromTopic({ topic: 'topic' });createChannel
Bagian berjudul ācreateChannelāBuat channel notifikasi.
Hanya tersedia untuk Android (SDK 26+).
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.createChannel({ id: 'id-123', name: 'example',});deleteChannel
Bagian berjudul ādeleteChannelāHapus channel notifikasi.
Hanya tersedia untuk Android (SDK 26+).
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
await FirebaseMessaging.deleteChannel({ id: 'id-123' });listChannels
Bagian berjudul ālistChannelsāTampilkan daftar channel notifikasi yang tersedia.
Hanya tersedia untuk Android (SDK 26+).
import { FirebaseMessaging } from '@capgo/capacitor-firebase-messaging';
const result = await FirebaseMessaging.listChannels();console.log(result);Referensi Tipe
Bagian berjudul āReferensi TipeāPermissionStatus
Bagian berjudul āPermissionStatusāexport interface PermissionStatus { /** * @since 0.2.2 */ receive: PermissionState;}IsSupportedResult
Bagian berjudul āIsSupportedResultāexport interface IsSupportedResult { /** * @since 0.3.1 */ isSupported: boolean;}GetTokenOptions
Bagian berjudul ā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
Bagian berjudul āGetTokenResultāexport interface GetTokenResult { /** * @since 0.2.2 */ token: string;}GetDeliveredNotificationsResult
Bagian berjudul āGetDeliveredNotificationsResultāexport interface GetDeliveredNotificationsResult { /** * @since 0.2.2 */ notifications: Notification[];}RemoveDeliveredNotificationsOptions
Bagian berjudul āRemoveDeliveredNotificationsOptionsāexport interface RemoveDeliveredNotificationsOptions { /** * @since 0.4.0 */ notifications: Notification[];}SubscribeToTopicOptions
Bagian berjudul āSubscribeToTopicOptionsāexport interface SubscribeToTopicOptions { /** * The name of the topic to subscribe. * * @since 0.2.2 */ topic: string;}UnsubscribeFromTopicOptions
Bagian berjudul āUnsubscribeFromTopicOptionsāexport interface UnsubscribeFromTopicOptions { /** * The name of the topic to unsubscribe from. * * @since 0.2.2 */ topic: string;}CreateChannelOptions
Bagian berjudul āCreateChannelOptionsāexport type CreateChannelOptions = Channel;DeleteChannelOptions
Bagian berjudul āDeleteChannelOptionsāexport interface DeleteChannelOptions { /** * The channel identifier. * * @since 1.4.0 */ id: string;}ListChannelsResult
Bagian berjudul āListChannelsResultāexport interface ListChannelsResult { channels: Channel[];}TokenReceivedListener
Bagian berjudul āTokenReceivedListenerāCallback untuk menerima event token diterima.
export type TokenReceivedListener = (event: TokenReceivedEvent) => void;Sumber Kebenaran
Bagian berjudul āSumber KebenaranāHalaman ini dibuat dari plugin src/definitions.ts. Jalankan ulang sync saat API publik berubah di upstream.
Lanjutkan dari Getting Started
Bagian berjudul āLanjutkan dari Getting StartedāJika Anda menggunakan Panduan Memulai untuk merencanakan operasi dashboard dan API, hubungkan dengan Ringkasan API untuk detail implementasi di API Overview, Pendahuluan untuk detail implementasi di Introduction, Kunci API untuk detail implementasi di API Keys, Perangkat untuk detail implementasi di Devices, dan Bundle untuk detail implementasi di Bundles.