What this guide covers
- Adding the
cometchat_push_notificationsplugin and initializing it. - Platform wiring: Gradle/Firebase on Android, capabilities on iOS.
- Requesting permission and registering tokens (FCM on Android, APNs on iOS) after login.
- Receiving pushes and letting the SDK show chat notifications.
- Handling notification taps, the conversation on screen, and badge counts.
- Testing and troubleshooting.
The
cometchat_push_notifications plugin replaces the previous approach of copying the UI Kit sample’s lib/notifications stack and hand-wiring PNRegistry / CometChatPushRegistry and native MainActivity / AppDelegate bridges. Token registration, notification display, delivery receipts, notification taps and badge management are handled inside the plugin.Upgrading from 1.x? 2.0 is a rewrite: your app now receives pushes through its own messaging library and forwards them, and calling moved out of this package. The API changes are in the package’s migration guide.
This guide covers chat notifications only. To ring incoming calls in every app state, see Push Ringing, which uses the
cometchat_voip package.How it works
- Android (FCM): Firebase issues the registration token and delivers the CometChat payload as a data message. Your
firebase_messaginghandler forwardsmessage.datatohandlePushNotification, and the plugin shows the notification. - iOS (APNs): Apple issues the APNs device token, and the system shows the chat alert from the APNs payload. If you use
firebase_messagingon iOS instead, see step 4. - CometChat’s role: The providers you add in the dashboard bind your registered tokens to the logged-in user so CometChat can route pushes on your behalf.
- The plugin’s role:
cometchat_push_notificationsregisters the token with CometChat, parses payloads, shows notifications on Android and sends delivery receipts. It builds oncometchat_sdk, which pub resolves for you.
Prerequisites
- The providers, Firebase project, and Apple/APNs credentials from Getting Started (this guide assumes those are done).
- Dart 3.10.8+, and the Flutter release that bundles it or newer. (The plugin’s pubspec floor of
flutter: >=3.3.0is looser than its Dart floor; the Dart one is what applies.) - Android:
minSdkVersion 24,compileSdkVersion 36. - iOS: iOS 13.0+, with CocoaPods installed:
cometchat_sdk, which this plugin brings in, builds through CocoaPods. - An app that also uses the Calls SDK needs its higher floors:
minSdk 26and iOS 15.1. See Setup. - A physical device — background delivery is unreliable on emulators/simulators.
Complete the Getting Started guide first — enable Push Notifications, add your providers (FCM for Android, APNs for iOS), and finish the Firebase/Apple setup. This guide covers only the Flutter app wiring.
1. Store your credentials
Keep the values from Getting Started somewhere your app can read them. Only the fields for the platforms you ship are needed:2. Add the plugin and configure the platform
Add the plugin topubspec.yaml. On Android also add Firebase Messaging (used to receive the FCM data messages):
cometchat_sdk is pulled in transitively. Run flutter pub get.
- Android
- iOS
With
google-services.json already in android/app/ (from Getting Started):- Add the Google Services plugin to the
pluginsblock inandroid/settings.gradle.kts:
- Apply the plugins in
android/app/build.gradle.kts:
-
Set
applicationIdto your package name and keepminSdk = 24or higher. Runflutterfire configureif you still needfirebase_options.dart. -
Add a notification icon to
android/app/src/main/res/drawable/(for exampleic_notification.png) and pass its name asandroidSmallIconin step 3. Android draws the small icon as a flat silhouette, so it must be white on transparent; a launcher icon shows as a grey square.
You do not need to add notification permissions to your
AndroidManifest.xml. The plugin declares what it needs, and Gradle merges it into your app automatically.3. Initialize the SDK
Initialize the plugin beforerunApp. On Android you also initialize Firebase and forward FCM messages to the plugin (including a background isolate handler).
- Android
- iOS
handlePushNotification returns true when it handled a chat push, and false for any other push, which is still yours to handle.
The CometChatNotificationConfig also accepts:
Android fixes a channel’s importance, sound and vibration when it’s first created. To change any of them later, use a new
androidChannelId.If your app also rings calls
If your app usescometchat_voip for incoming calls, hand each push to it first, and to this plugin only when it isn’t a call. In the background handler, initialize this plugin first, as above; cometchat_voip needs nothing there:
4. Request permission and register tokens
Request permission early, and register tokens after your CometChat user logs in (registration binds the token to the session):- Android
- iOS
registerPushToken returns the server’s response. It throws a CometChatNotificationException when nobody is logged in to the Chat SDK or the server refuses the token, so catch it where you call it.
Using firebase_messaging on iOS? Your token is then an FCM token, and platform must say so. Without platform, the plugin assumes APNs on iOS, and CometChat sends pushes to a provider that doesn’t know the device:
5. Notification taps and the conversation on screen
Subscribe to taps to open the right conversation (same on both platforms). A tap that launched the app from a cold start is held until you subscribe:6. Badge count
CometChat’s Enhanced Push Notification payload includes anunreadMessageCount field (the total unread across all conversations). Enable it once on the dashboard:
- Go to CometChat Dashboard → Notification Engine → Settings → Preferences → Push Notification Preferences.
- Enable the Unread Badge Count toggle.
- Android
- iOS
Android has no app icon badge.
setBadgeCount and getBadgeCount do nothing there.conversationId to clear only that conversation’s notifications. Add a WidgetsBindingObserver to your root widget and clear in initState and on AppLifecycleState.resumed.
7. Testing checklist
- Run on a physical device. Grant notification permission when prompted (Android 13+ requires
POST_NOTIFICATIONS). - Send a message from another user:
- Foreground: no banner unless
showInAppNotifications: true(and you’re not already in that chat). - Background: a notification appears; tapping opens the right conversation via
onNotificationTap.
- Foreground: no banner unless
- Force-quit the app, send another message, tap the notification, and confirm it navigates to the conversation.
- Open a conversation and send a message to it from another user: no notification is shown for it.
- Toggle Wi-Fi/cellular and reinstall to confirm token registration survives refreshes.
8. Troubleshooting
Resources
cometchat_push_notifications
The drop-in chat push plugin on pub.dev.
SDK source
Source, changelog, and example app.