Back to course

How to configure Firebase (FCM) in Traccar for Push Notifications

Step-by-step guide to configure Firebase Cloud Messaging (FCM) on your Traccar server and enable push notifications for your Android and iOS mobile apps.

Emmanuel Díaz Leal Hernández

May 25, 2024
Intermediate

Push notifications are essential for any modern tracking application. Traccar uses Firebase Cloud Messaging (FCM) to send these alerts to mobile devices efficiently and for free.

Prerequisites

  1. Have an account at Firebase Console.
  2. Have a Firebase project configured.
  3. If using a custom App, have the google-services.json (Android) or GoogleService-Info.plist (iOS) file integrated into your source code.

Traccar Server Configuration

For the Traccar server to be able to send alerts to Firebase, we must configure the "Server Key" in the traccar.xml file.

1. Get the Firebase Key

  1. Enter your project in the Firebase Console.
  2. Click on the gear icon (Project Settings).
  3. Go to the Cloud Messaging tab.
  4. Copy the Server Key (if using the legacy API) or set up a Service Account for the v1 API (recommended).

2. Edit traccar.xml

Add the following entries to your configuration file:

<!-- Enable firebase notification type -->
<entry key='notificator.types'>web,mail,firebase</entry>

<!-- Firebase Server Key -->
<entry key='notificator.firebase.key'>YOUR_SERVER_KEY_HERE</entry>

Note: If you are using the new Firebase API, the process requires a service account JSON file, but most Traccar installations still use the classic Server Key.

How notifications reach the user

For a user to receive the push notification, two conditions must be met:

  1. Device Token: The mobile application must register the phone's "FCM Token" in the user attribute within Traccar. This happens automatically when the user logs in to the official app or a well-configured one.
  2. Attributes: The attribute in the database is generally saved as fcmToken.

Testing the Setup

You can verify if the configuration is correct from the Traccar panel:

  1. Go to Settings -> Notifications.
  2. Select a notification and click the "Test" icon (the bell).
  3. If the server is correctly configured and the user has a valid token, the phone should receive the alert instantly.

Common Troubleshooting

  • Notification not arriving: Verify that the phone has notification permissions enabled for the App and is not in an aggressive "Battery Saver" mode.
  • Log Errors: Check the tracker-server.log file. If you see "401 Unauthorized" errors, it means your notificator.firebase.key is incorrect or has expired.
  • Token not registered: If the fcmToken attribute does not appear in the user, the mobile application is not communicating correctly with Firebase.

Properly configuring Firebase ensures your users never miss an important alert, improving security and trust in your tracking service.

Comentarios (0)