capacitor-push-notifications
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCapacitor Push Notifications
Capacitor 推送通知
Set up and use push notifications in Capacitor apps using Firebase Cloud Messaging (FCM) via the plugin.
@capacitor-firebase/messaging通过插件,在Capacitor应用中配置并使用Firebase Cloud Messaging(FCM)推送通知。
@capacitor-firebase/messagingPrerequisites
前提条件
- Capacitor 6, 7, or 8 app.
- Node.js and npm installed.
- A Firebase project. Create one at Firebase console if needed.
- For iOS: A paid Apple Developer Program membership and Xcode installed.
- For Android: Android Studio installed.
- must not be installed — it conflicts with
@capacitor/push-notifications.@capacitor-firebase/messaging
- Capacitor 6、7或8 应用。
- 已安装Node.js和npm。
- 一个Firebase项目。如果没有,可以在Firebase控制台创建。
- 针对iOS:需拥有付费的Apple Developer Program会员资格,并已安装Xcode。
- 针对Android:已安装Android Studio。
- 不得安装——它会与
@capacitor/push-notifications冲突。@capacitor-firebase/messaging
Agent Behavior
操作指引原则
- Guide step-by-step. Walk the user through the process one step at a time. Never present multiple unrelated questions at once.
- Auto-detect before asking. Check the project for platforms (,
android/), build tools, framework, andios/dependencies. Only ask the user when something cannot be detected.package.json - One decision at a time. When a step requires user input, ask that single question, wait for the answer, then continue.
- Present clear options. Provide concrete choices (e.g., "Do you want to configure topic subscriptions? (yes/no)") instead of open-ended questions.
- 分步引导:逐步引导用户完成流程。切勿同时提出多个不相关的问题。
- 先自动检测再询问:检查项目中的平台目录(、
android/)、构建工具、框架以及ios/中的依赖项。只有当无法自动检测到信息时,才向用户询问。package.json - 一次一个决策:当步骤需要用户输入时,只提出单个问题,等待用户回答后再继续。
- 提供明确选项:给出具体的选择(例如:"是否要配置主题订阅?(是/否)"),而非开放式问题。
Procedures
操作步骤
Step 1: Analyze the Project
步骤1:分析项目
Auto-detect the following by reading project files — do not ask the user for information that can be inferred:
- Platforms: Check which directories exist (,
android/). These are the platforms to configure.ios/ - Build tool / framework: Check for ,
vite.config.ts,angular.json,webpack.config.js, etc.next.config.js - Capacitor version: Read version from
@capacitor/core.package.json - Conflicting plugins: Check if is in
@capacitor/push-notifications. If found, warn the user it must be removed before proceeding:package.jsonbashnpm uninstall @capacitor/push-notifications
通过读取项目文件自动检测以下信息——不要向用户询问可通过推断获取的信息:
- 平台:检查是否存在、
android/目录,这些是需要配置的平台。ios/ - 构建工具/框架:检查是否存在、
vite.config.ts、angular.json、webpack.config.js等文件。next.config.js - Capacitor版本:从中读取
package.json的版本。@capacitor/core - 冲突插件:检查中是否存在
package.json。如果发现,需警告用户必须先卸载该插件才能继续:@capacitor/push-notificationsbashnpm uninstall @capacitor/push-notifications
Step 2: Set Up Firebase
步骤2:配置Firebase
Check if Firebase is already configured in the project:
- Android: Check if exists.
android/app/google-services.json - iOS: Check if exists.
ios/App/App/GoogleService-Info.plist
If Firebase is not configured for a detected platform, read and guide the user through the Firebase setup for each missing platform.
references/firebase-setup.md检查项目中是否已配置Firebase:
- Android:检查是否存在。
android/app/google-services.json - iOS:检查是否存在。
ios/App/App/GoogleService-Info.plist
如果某个已检测到的平台未配置Firebase,请阅读并引导用户完成该平台的Firebase配置。
references/firebase-setup.mdStep 3: Install the Plugin
步骤3:安装插件
bash
npm install @capacitor-firebase/messaging firebase
npx cap syncbash
npm install @capacitor-firebase/messaging firebase
npx cap syncStep 4: Configure Android
步骤4:配置Android
Skip if does not exist.
android/Read and apply the Android-specific configuration.
references/android-setup.md如果目录不存在则跳过。
android/阅读并完成Android专属配置。
references/android-setup.mdStep 5: Configure iOS
步骤5:配置iOS
Skip if does not exist.
ios/Read and apply the iOS-specific configuration. This includes APNs key/certificate setup, modifications, and enabling capabilities.
references/ios-setup.mdAppDelegate.swift如果目录不存在则跳过。
ios/阅读并完成iOS专属配置,包括APNs密钥/证书配置、修改以及启用相关功能权限。
references/ios-setup.mdAppDelegate.swiftStep 6: Configure Web (if applicable)
步骤6:配置Web(如适用)
If the project targets the web (detected via build tool config or user confirmation):
Read and apply the Web-specific configuration.
references/web-setup.md如果项目目标平台包含Web(通过构建工具配置或用户确认检测到):
阅读并完成Web专属配置。
references/web-setup.mdStep 7: Configure Capacitor Plugin Options
步骤7:配置Capacitor插件选项
Ask the user if they want to customize iOS foreground notification presentation. If yes, update or :
capacitor.config.jsoncapacitor.config.tsjson
{
"plugins": {
"FirebaseMessaging": {
"presentationOptions": ["alert", "badge", "sound"]
}
}
}Available options: , , , . Default is .
badgesoundalertcriticalAlert["alert", "badge", "sound"]询问用户是否要自定义iOS前台通知展示方式。如果是,更新或:
capacitor.config.jsoncapacitor.config.tsjson
{
"plugins": {
"FirebaseMessaging": {
"presentationOptions": ["alert", "badge", "sound"]
}
}
}可用选项:、、、。默认值为。
badgesoundalertcriticalAlert["alert", "badge", "sound"]Step 8: Add Push Notification Code
步骤8:添加推送通知代码
Read and add the push notification code to the project. Adapt imports and structure to match the user's framework.
references/implementation.mdThe implementation covers:
- Requesting permissions
- Retrieving the FCM token
- Listening for incoming notifications
- Handling notification taps
阅读并将推送通知代码添加到项目中。根据用户的框架调整导入语句和代码结构。
references/implementation.md实现内容包括:
- 申请权限
- 获取FCM令牌
- 监听传入通知
- 处理通知点击事件
Step 9: Configure Optional Features
步骤9:配置可选功能
Ask the user which optional features to enable:
- Topic subscriptions — Subscribe/unsubscribe to FCM topics (Android/iOS only).
- Notification channels — Create custom Android notification channels (Android SDK 26+ only).
- Token refresh listener — Listen for FCM token changes.
For each selected feature, read and apply the relevant code.
references/implementation.md询问用户要启用哪些可选功能:
- 主题订阅——订阅/取消订阅FCM主题(仅支持Android/iOS)。
- 通知渠道——创建自定义Android通知渠道(仅支持Android SDK 26+)。
- 令牌刷新监听器——监听FCM令牌变化。
对于每个选中的功能,阅读并添加相关代码。
references/implementation.mdStep 10: Sync and Test
步骤10:同步并测试
-
Sync the project:bash
npx cap sync -
Readand guide the user through sending a test notification via the Firebase Console.
references/testing.md
-
同步项目:bash
npx cap sync -
阅读并引导用户通过Firebase控制台发送测试通知。
references/testing.md
Error Handling
错误处理
- conflict: The
@capacitor/push-notificationsplugin cannot coexist with@capacitor-firebase/messaging. Uninstall the conflicting plugin:@capacitor/push-notifications.npm uninstall @capacitor/push-notifications && npx cap sync - iOS: No push notifications received: Verify APNs key/certificate is uploaded to Firebase Console. Verify Push Notifications and Background Modes capabilities are enabled. Verify contains the required delegate methods.
AppDelegate.swift - iOS: not called: Ensure the Push Notifications capability is added in Xcode under Signing & Capabilities. Check that the app's bundle ID matches the one registered in Firebase and Apple Developer portal.
didRegisterForRemoteNotificationsWithDeviceToken - Android: No push notifications received: Verify is at
google-services.json. Verify the Google services Gradle plugin is applied.android/app/google-services.json - Android: White square notification icon: The notification icon must be white pixels on a transparent background. Application icons with color will render as a white square. Add a dedicated push notification icon.
- Web: fails: Ensure the VAPID key is correct. Ensure
getToken()exists at the root of the domain. Check that the browser supports the Push API.firebase-messaging-sw.js - FCM token is : Ensure
nullwas called and returnedrequestPermissions()before callinggranted. On iOS, verify the device is not a simulator (simulators cannot receive push notifications).getToken() - returns
checkPermissions(): The user has permanently denied notification permissions. Guide them to re-enable via device settings (Settings > App > Notifications).denied - Android 13+: No permission prompt: On Android 13 (API 33) and above, must be called explicitly. Earlier Android versions grant notification permission by default.
requestPermissions()
- 冲突:
@capacitor/push-notifications插件无法与@capacitor-firebase/messaging共存。请卸载冲突插件:@capacitor/push-notifications。npm uninstall @capacitor/push-notifications && npx cap sync - iOS:未收到推送通知:验证APNs密钥/证书是否已上传至Firebase控制台。验证是否已启用“推送通知”和“后台模式”权限。验证是否包含所需的代理方法。
AppDelegate.swift - iOS:未被调用:确保在Xcode的“签名与功能”中已添加“推送通知”功能。检查应用的Bundle ID是否与Firebase和Apple开发者门户中注册的一致。
didRegisterForRemoteNotificationsWithDeviceToken - Android:未收到推送通知:验证是否位于
google-services.json路径下。验证是否已应用Google services Gradle插件。android/app/google-services.json - Android:通知图标显示为白色方块:通知图标必须是透明背景上的白色像素。带颜色的应用图标会显示为白色方块。请添加专用的推送通知图标。
- Web:调用失败:确保VAPID密钥正确。确保
getToken()位于域名根目录。检查浏览器是否支持Push API。firebase-messaging-sw.js - FCM令牌为:确保在调用
null之前已调用getToken()且返回requestPermissions()。在iOS上,验证设备不是模拟器(模拟器无法接收推送通知)。granted - 返回
checkPermissions():用户已永久拒绝通知权限。引导用户通过设备设置重新启用(设置 > 应用 > 通知)。denied - Android 13+:未弹出权限提示:在Android 13(API 33)及以上版本中,必须显式调用。早期Android版本默认授予通知权限。
requestPermissions()