Web firebase.messaging().onMessage not fired, but background notification perfectly fired

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Unraveling the Mystery: Foreground Notification Issues with Firebase Messaging Introduction: The use of Firebase for push notifications is quite popular among developers due to its ease of integration and effective functionality. However, sometimes you might encounter issues where your foreground notifications fail to fire despite a perfectly functioning background notification system. In this blog post, we shall explore the potential causes and resolutions for such scenarios without compromising the background notification functionality. I. Checking the Firebase Setup: Ensure that the `firebase.js` file is correctly configured with proper API keys, project IDs, storage buckets, and messaging sender IDs as shown in the code snippet above. Double-check the syntax to avoid any typographical errors that might prevent your application from receiving messages. II. Requesting Permission: To ensure that foreground notifications work, you must request permission from the user during installation or app update. The snippet provided requests permission, which is then followed by getting a Firebase token. This token is a unique identifier for your device and is used to send push messages to your application. III. Foreground Messaging Handler: To achieve foreground notification functionality, you need to set up a message handler that handles incoming messages in the foreground. In the provided code, `msg.onMessage()` is called with a function that displays an alert and logs the payload containing the received data. However, if this doesn't work as expected, check whether the Firebase configuration variables are consistent across the `firebase.js` file and your web app. IV. Background Messaging Handler: The background messaging handler is already set up in `firebase.messaging.sw.js`. It receives a payload containing data such as title, body, and icon. This payload is passed to `registration.showNotification()` to show the notification on the web app's screen. V. Understanding the Functionality: While both foreground and background notifications are handled in separate files, they have different purposes. The foreground messaging handler is triggered only when your application is actively running in the foreground. This allows real-time interaction with users as their application receives push messages. In contrast, the background messaging handler is used to notify users about new notifications without affecting the application's performance or battery life. VI. Debugging Tips: To troubleshoot and resolve your issue, use Firebase's built-in debugging tools. You can monitor your notification delivery with the Firebase Console and examine any errors that might have occurred during the message processing. Additionally, you can use the Firebase Cloud Messaging (FCM) Testing Tool to send test messages and check if they are received as expected in both foreground and background modes. Conclusion: The combination of a well-configured Firebase setup, permission requests, and appropriate messaging handlers ensures seamless push notification functionality for your application. If still facing issues with the foreground notification functioning properly, debugging and testing can provide valuable insights into the problem's root cause. To learn more about Firebase and its capabilities, visit our website at https://laravelcompany.com for comprehensive guides and tutorials.