Langsung ke konten

Panduan Memulai

GitHub

@capgo/capacitor-notifications adalah plugin Capgo first-party untuk push notification native iOS dan Android. Plugin ini dibuat untuk dashboard Capgo, API publik, device registry Analytics Engine, statistik campaign, pembaruan badge, dan pengecekan live update secara silent.

  • Aplikasi Capacitor yang sudah ditambahkan ke Capgo.
  • Akses ke tab Notifications pada aplikasi Capgo.
  • Key Capgo API dengan akses write untuk minting proof di backend dan pengiriman API.
  • Otoritas push platform iOS dan/atau Android untuk aplikasi tersebut.
  • @capgo/capacitor-updater jika Anda ingin pengecekan update lewat silent push.
import { CapacitorNotifications } from '@capgo/capacitor-notifications';
await CapacitorNotifications.register({ customerUserId: 'signed-id' });
  • Gunakan nilai customerUserId yang sudah di-sign dari backend Anda.
  • Minta izin notifikasi sebelum mendaftar.
  • Verifikasi kredensial Capgo di dashboard.

Buka aplikasi di Capgo, lalu masuk ke Notifikasi.

Tambahkan satu entri kredensial platform untuk setiap platform yang ingin Anda dukung:

  • Android - app package ID dan metadata proyek push Android.
  • iOS - bundle ID, team ID, key ID, dan metadata key push iOS yang cocok.

Capgo menunjukkan nama persis secret environment yang harus ada di worker API sebelum platform ditandai terkonfigurasi. Dashboard menyimpan metadata dan referensi secret yang diharapkan. Kredensial privat itu sendiri tetap berada di environment worker.

Untuk setup tercepat, jalankan Capgo CLI dari proyek aplikasi Anda:

Jendela terminal
npx @capgo/cli@latest notifications setup com.example.app

Perintah ini menginstal package notification, menyimpan konfigurasi plugin Capacitor, membuat file helper kecil, dan menjalankan Capacitor sync. Gunakan jalur ini untuk aplikasi baru kecuali Anda perlu menyambungkan setiap file secara manual.

Instalasi manual:

Jendela terminal
bun add @capgo/capacitor-notifications @capgo/capacitor-updater
bunx cap sync

Jika Anda tidak memakai pengecekan update Capgo secara silent, Anda bisa melewati @capgo/capacitor-updater.

Konfigurasikan plugin sekali saat aplikasi Anda mulai.

import { CapgoNotifications } from '@capgo/capacitor-notifications'
await CapgoNotifications.configure({
appId: 'com.example.app',
autoUpdater: true,
updateInstallMode: 'next',
})

Gunakan updateInstallMode: 'next' untuk mengunduh update dan menginstalnya pada restart atau siklus background berikutnya. Gunakan updateInstallMode: 'set' hanya jika Anda ingin Capgo menginstal update segera setelah updater bisa melakukannya dengan aman.

Jangan masukkan kunci Capgo API Anda ke dalam aplikasi mobile. Backend Anda harus meminta Capgo untuk identityProof setelah autentikasi pengguna Anda berhasil.

Jendela terminal
curl -X POST 'https://api.capgo.app/notifications/recipients/proof' \
-H 'Content-Type: application/json' \
-H 'x-api-key: CAPGO_API_KEY' \
-d '{
"appId": "com.example.app",
"externalId": "customer-user-123"
}'

Kembalikan identityProof ke aplikasi melalui respons sesi Anda sendiri.

Daftarkan hanya setelah Anda tahu pengguna pelanggan mana yang sedang masuk.

const registration = await CapgoNotifications.register({
externalId: 'customer-user-123',
identityProof,
tags: ['paid', 'beta'],
attributes: {
plan: 'team',
locale: 'en-US',
},
consent: true,
})
console.log(registration.recipientKey, registration.deviceKey)

Panggil register lagi saat:

  • Aplikasi dimulai.
  • Native push token berubah.
  • User yang login berubah.
  • Tags, atribut, atau consent berubah.
  • Aplikasi sudah lama tidak refresh registrasi.

Daftarkan listener saat startup aplikasi agar event foreground, opened, dan background dapat diakses oleh JavaScript.

await CapgoNotifications.addListener('registrationChanged', () => {
void CapgoNotifications.register({
externalId: currentUser.id,
identityProof: currentUser.capgoNotificationProof,
tags: currentUser.notificationTags,
consent: currentUser.pushConsent,
})
})
await CapgoNotifications.addListener('notificationReceived', (notification) => {
console.log('Notification received', notification)
})
await CapgoNotifications.addListener('notificationOpened', (event) => {
console.log('Notification opened', event.notification.id)
})
await CapgoNotifications.addListener('backgroundNotification', async (event) => {
try {
console.log('Background notification', event.notification.data)
} finally {
await event.finish()
}
})

Selalu panggil finish() untuk notifikasi background setelah pekerjaan selesai. Pastikan prosesnya singkat dan idempotent.

Di Xcode, buka target aplikasi dan aktifkan:

  • Notifikasi Push
  • Mode Latar > Notifikasi jarak jauh

Teruskan notifikasi remote dari ios/App/App/AppDelegate.swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NotificationCenter.default.post(name: Notification.Name("CapgoNotificationsRemoteNotification"), object: userInfo)
completionHandler(.newData)
}

Lalu jalankan:

Jendela terminal
bunx cap sync ios

Gunakan perangkat iOS fisik saat menguji notifikasi background. Simulator berguna untuk pekerjaan UI, tetapi tidak menggambarkan perilaku push background di produksi.

Jalankan:

Jendela terminal
bunx cap sync android

Lalu verifikasi:

  • Kredensial platform Android Anda sudah dikonfigurasi di Capgo.
  • Package ID aplikasi cocok dengan package ID yang dipakai untuk setup push platform.
  • Izin notifikasi Android 13+ perlu diminta sebelum notifikasi bisa tampil.
  • Aplikasi memiliki ikon notifikasi dan strategi channel yang sesuai dengan brand Anda.
  • Anda menguji pada perangkat fisik atau emulator dengan Google Play services.

Buat channel Android default saat aplikasi Anda dimulai:

await CapgoNotifications.configure({ appId: 'com.example.app' })
await CapgoNotifications.register({
externalId: currentUser.id,
identityProof: currentUser.capgoNotificationProof,
consent: true,
})

Plugin ini mendeklarasikan layanan push messaging Android. Pertahankan backup aplikasi, ekstraksi data, keamanan jaringan, dan kebijakan ikon notifikasi di host app.

Gunakan Notifikasi > Kirim tes di Capgo, atau panggil API publik dari backend Anda:

Jendela terminal
curl -X POST 'https://api.capgo.app/notifications/send' \
-H 'Content-Type: application/json' \
-H 'x-api-key: CAPGO_API_KEY' \
-d '{
"appId": "com.example.app",
"target": { "externalId": "customer-user-123" },
"payload": {
"title": "Hello from Capgo",
"body": "This is a test notification.",
"data": { "screen": "inbox" }
}
}'

Untuk kampanye, buat di dashboard atau panggil /notifications/campaigns, lalu kirim ke ID eksternal, tag, segmen, atau audiens broadcast.

await CapgoNotifications.setBadge(4)
await CapgoNotifications.incrementBadge()
await CapgoNotifications.clearBadge()

Dari backend Anda:

Jendela terminal
curl -X POST 'https://api.capgo.app/notifications/badge' \
-H 'Content-Type: application/json' \
-H 'x-api-key: CAPGO_API_KEY' \
-d '{
"appId": "com.example.app",
"target": { "externalId": "customer-user-123" },
"badge": 4
}'

Silent update checks menghubungkan plugin ini dengan @capgo/capacitor-updater.

Di dalam aplikasi:

await CapgoNotifications.enableUpdaterIntegration({
enabled: true,
installMode: 'next',
})

Di Capgo, aktifkan Push update ke pengguna di pengaturan Notifications aplikasi. Kemudian kirim update check dari dashboard atau API:

Jendela terminal
curl -X POST 'https://api.capgo.app/notifications/update-check' \
-H 'Content-Type: application/json' \
-H 'x-api-key: CAPGO_API_KEY' \
-d '{
"appId": "com.example.app",
"target": { "externalId": "customer-user-123" },
"installMode": "next"
}'

Notifikasi bersifat silent dan menggunakan collapse ID sehingga update check yang berulang akan saling menggantikan jika platform mendukung perilaku collapse.

  • Aplikasi muncul di pencarian penerima Capgo untuk externalId.
  • Izin adalah granted atau pengguna telah menerima izin notifikasi.
  • Platform yang terdaftar adalah android atau ios.
  • registrationChanged terpicu setelah refresh token.
  • Tes foreground mencatat notificationReceived.
  • Membuka notifikasi mencatat notificationOpened.
  • Statistik Dashboard menampilkan event yang di-queue dan terkirim, lalu received/opened saat perangkat melaporkannya.
  • Pemeriksaan silent update mencatat hasil dari runUpdateCheck atau integrasi updater.

Jika setup tidak berfungsi, gunakan Debugging sebelum mengubah app code. Kebanyakan kegagalan disebabkan oleh ketidakcocokan bukti identitas, setup kredensial platform, status izin OS, throttling background, atau ketidakcocokan ID app/package.