evanca/flutter-ai-rules/firebase-messaging
Claude Code and agent skill by evanca/flutter-ai-rules
- community
Checking that the SKILL.md file is still in the repository.
Use when setting up Firebase Cloud Messaging, managing permissions and tokens, handling background/foreground notification taps, or dispatching messages server-side (HTTP v1).
Install
npx vskill@latest install evanca/flutter-ai-rules/firebase-messagingSKILL.md
Firebase Cloud Messaging Skill
This skill defines how to correctly use Firebase Cloud Messaging (FCM) in Flutter applications.
When to Use
Use this skill when:
- Setting up push notifications with FCM in a Flutter project.
- Handling messages in foreground, background, and terminated states.
- Managing notification permissions and FCM tokens.
- Configuring platform-specific notification display behavior.
1. Setup and Configuration
iOS:
- Enable Push Notifications and Background Modes in Xcode.
- Upload your APNs authentication key to Firebase before using FCM.
- Do not disable method swizzling — it is required for FCM token handling.
- Ensure the bundle ID for your APNs authentication key matches your app's bundle ID.
Android:
- Devices must run Android 4.4+ with Google Play services installed.
- Check for Google Play services compatibility in both
onCreate()andonResume().
Web:
- Create and register a service worker file named
firebase-messaging-sw.jsin yourweb/directory:
2. Message Handling
Foreground messages:
Background messages:
Background handler rules:
- Must be a top-level function (not anonymous, not a class method).
- Annotate with
@pragma('vm:entry-point')(Flutter 3.3.0+) to prevent removal during tree shaking in release mode. - Cannot update app state or execute UI-impacting logic — runs in a separate isolate.
- Call
Firebase.initializeApp()before using any other Firebase services.
3. Permissions
- iOS / macOS / Web / Android 13+: Must request permission before receiving FCM payloads.
- Android < 13:
authorizationStatusreturnsauthorizedif the user has not disabled notifications in OS settings. - Android 13+: Track permission requests in your app — there's no way to determine if the user chose to grant/deny.
- Use provisional permissions on iOS (
provisional: true) to let users choose notification types after receiving their first notification.
4. Token Management
Get FCM registration token (use to send messages to a specific device):
Web — provide VAPID key:
Listen for token refresh:
Apple platforms — ensure APNS token is available before FCM calls:
Token Lifecycle (Auth State): Tokens should be tied to user sessions. Save the token to your database when a user signs in, and delete the token (or remove it from the user's document) when they sign out. An FCM token is device-specific, not inherently tied to user auth data — failing to clear it on sign-out means the next user on that device might receive the previous user's notifications.
5. Platform-Specific Behavior
- iOS: If the user swipes away the app from the app switcher, it must be manually reopened for background messages to work again.
- Android: If the user force-quits from device settings, the app must be manually reopened.
- iOS foreground notifications: Update presentation options to display notifications while the app is in the foreground:
``dart await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, ); ``
- Android foreground notifications: Notification messages arriving while the app is in the foreground won't display a visible notification by default. You must consume the payload via the
onMessagestream and manually display a visual cue (using your own UI logic or a local notifications plugin). - Android default channel: To set a default channel for background notifications, add this
meta-datato your<application>block inAndroidManifest.xml:
``xml <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="high_importance_channel" /> ``
6. Auto-Initialization Control
Disable auto-init — iOS (Info.plist):
Disable auto-init — Android (AndroidManifest.xml):
Re-enable at runtime:
- The auto-init setting persists across app restarts once set.
7. iOS Image Notifications
Important: The iOS simulator does not display images in push notifications. Test on a physical device.
- Add a Notification Service Extension in Xcode.
- Use
Messaging.serviceExtension().populateNotificationContent()in the extension for image handling. - Swift: add the
FirebaseMessagingSwift package to your extension target. - Objective-C: add the
Firebase/Messagingpod to your Podfile.
8. Notification Interaction Handling
When a user taps a notification, the app opens (or is brought to the foreground). Handle the interaction in both cases:
App was terminated:
App was in background:
Always handle both scenarios to ensure a smooth user experience regardless of app state when the notification was received.
9. Topic Messaging
- Subscribing to a topic allows sending messages to multiple devices that have opted in.
- Topic messages are best suited for publicly available information (e.g., weather updates), optimized for throughput rather than latency.
Note:subscribeToTopic()andunsubscribeFromTopic()are not supported for web clients via the Flutter plugin.
10. Sending a Test Message
The official Firebase documentation often obscures the exact steps for sending a test push notification. To fire a push (test or real) using the Firebase Console:
- Obtain your device's FCM registration token (see Section 4).
- Go to the Firebase Console and select your project.
- In the left navigation panel, find the Engage (or Run) section and click Messaging (or Cloud Messaging).
- Click New campaign and select Notifications.
- Enter a Notification title and Notification text.
- Click Send test message (often a button on the right side of the screen).
- In the dialog, enter your FCM registration token and click the
+icon to add it. - Make sure the token is checked, then click Test.
To send real automated push notifications to production users, you must use a server implementation (via the FCM HTTP v1 API or the Firebase Admin SDK) rather than the console.
11. Server-Side Credentials & Security
The legacy FCM server key endpoint was deprecated in June 2024 — HTTP v1 is the only supported option for sending pushes.
To authenticate server-to-server calls for HTTP v1, you need a Service Account:
- Go to Firebase Console → Project settings → Service accounts.
- Click Generate new private key (downloads a
.jsonfile). - CRITICAL: This file contains highly sensitive secrets and must never be committed to git.
- Store the file securely (e.g., in a Secret Manager) or pass its stringified contents as an environment variable (like
FIREBASE_SERVICE_ACCOUNT) to your backend.
12. Sending Messages (HTTP v1)
To send an FCM HTTP v1 message, your backend must:
- Complete an OAuth2 JWT exchange (sign with RS256, scope
https://www.googleapis.com/auth/firebase.messaging, endpointhttps://oauth2.googleapis.com/token). - Construct and
POSTa JSON payload tohttps://fcm.googleapis.com/v1/projects/{project_id}/messages:send.
Here is a minimal, complete working example using Node.js and the google-auth-library:
References
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/firebase-messaging/SKILL.md
Popularity
- Repository stars
- 643
- Repository forks
- 66
- 7-day trend score
- 32
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 21 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/evanca/flutter-ai-rules/firebase-messaging)