Repository navigation
FCM Topic Notifications Are Not Consistently Saved to Hive in the Background #18772
|
I am developing a Flutter application that uses Firebase Cloud Messaging (FCM) to receive push notifications. I want to save all received notifications to a local Hive database so users can view their notification history inside the app. However, I am experiencing inconsistent behavior on iOS when sending notifications directly to a device token versus sending them through FCM Topics. Sometimes notifications are saved to Hive as expected, but sometimes they are not. I cannot consistently reproduce the behavior or understand what determines whether the background message handler executes. When notifications are sent directly to a device token, they generally work as expected. However, notifications sent through our production panel using FCM Topics may appear on the iOS lock screen without being saved to Hive. Expected Behavior Ideally, the notification history should remain consistent regardless of whether the notification is sent directly to a device token or through an FCM Topic. Actual Behavior FCM Payload { Background Message Handler @pragma('vm:entry-point') await Firebase.initializeApp(); final box = await Hive.openBox( // Convert the RemoteMessage into a NotificationModel. await box.flush(); Questions
Additional Information What I Have Tried Expected Outcome In particular, I would like to understand why the behavior is inconsistent — sometimes notifications are saved successfully, while other times they are only displayed by iOS and never appear in the app's notification history — and whether this is related to FCM Topic delivery, APNs configuration, or iOS background execution limitations. Thank you for your help! |
Replies: 2 comments
|
The lock-screen alert and the Hive write are two separate outcomes. iOS can present the alert without giving your app enough background execution to persist it. Seeing the notification therefore does not prove that the Dart handler ran. Token versus topic: changing the target to a topic does not add a background-execution guarantee. Before diagnosing a topic-specific problem, compare the actual HTTP v1 JSON emitted by your panel with the curl request, changing only the target. The panel payload is missing here, so a payload difference cannot yet be ruled out. App states and reopening: foreground messages use
Your APNs payload: it contains an alert and sound, so
For reliable in-app history, I would persist the inbox on your backend before sending the push. Include a stable event ID, then sync the authenticated user's inbox on startup/resume and upsert into Hive by that ID. Keep the background handler as a best-effort fast path. This lets the app recover missing entries after reopening, even when background processing was skipped. That inbox records intended notifications; it is not proof that iOS displayed each alert. To isolate any additional Hive issue, log handler entry and the completion/error of Firebase init, Hive init/open, model conversion, put and flush, without recording tokens or private message contents. If there is no handler-entry log, changing the Hive write cannot fix that particular miss. If the handler does run but fails, the omitted Hive setup/model code needs investigation. A sanitized panel payload, package/iOS versions, and logs from the same physical device in each app state would help distinguish those cases. I haven't reproduced your app, so I wouldn't label this a confirmed topic or Hive bug yet. |
|
Thanks for the detailed explanation! The distinction between displaying a notification and executing the background handler makes the issue much clearer. I agree that comparing the actual payloads and logging handler execution and Hive write results are the right first steps. Using a backend-backed notification inbox and syncing missing entries into Hive on app startup or resume also sounds like the most reliable approach for maintaining consistent notification history on iOS. Thanks for sharing these insights! |
The lock-screen alert and the Hive write are two separate outcomes. iOS can present the alert without giving your app enough background execution to persist it. Seeing the notification therefore does not prove that the Dart handler ran.
Token versus topic: changing the target to a topic does not add a background-execution guarantee. Before diagnosing a topic-specific problem, compare the actual HTTP v1 JSON emitted by your panel with the curl request, changing only the target. The panel payload is missing here, so a payload difference cannot yet be ruled out.
App states and reopening: foreground messages use
onMessage. Background processing is subject to platform restrictions. In particul…