Are Firebase errors fatal?

Iván Ávalos2 years ago

When Traccar replaced FCM token with service account, devices were always disconnecting because of an exception. (I fixed that by adding the proper service account JSON.) Now, it seems like it's happening when there is an empty token in the tokens string. This is problematic, because I'd rather have notifications not arrive, than have the device disconnected from the server. In other words, the FCM-related exceptions should be catched and only logged, but allow the thread to continue its execution.

2022-08-16 08:06:31  INFO: [Td7485b9b] error - none of the tokens can be null or empty - IllegalArgumentException (... < NotificatorFirebase:83 < NotificationManager:104 < ... < *:101 < ... < *:100 < *:124 < BaseEventHandler:40 < ...)
2022-08-16 08:06:31  INFO: [Td7485b9b] disconnected
Anton Tananaev2 years ago

Why do you have an empty token?

Iván Ávalos2 years ago

There is a comma at the start of the token string, not sure if there are also two consecutive commas in between the string. Anyway, that's not the point; the point is that the thread shouldn't die, no matter how messed up the token string is or how badly configured Firebase is.

Iván Ávalos2 years ago

Anyway, I thought that, as a temporary measure, I would simply sanitize the tokens string to remove accidentally empty ones, so it doesn't fail and kill the whole thread. It seems to work.

From c48b0f67d93bbc8aa7e94a7ee06082386d08a101 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Iv=C3=A1n=20=C3=81valos?= <avalos@disroot.org>
Date: Wed, 31 Aug 2022 22:39:28 -0500
Subject: [PATCH] Fixed exception when a FCM token is empty

---
 .../org/traccar/notificators/NotificatorFirebase.java     | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
index ecf3fbb70..b5ee6a03d 100644
--- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java
+++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java
@@ -25,6 +25,7 @@ import com.google.firebase.messaging.FirebaseMessaging;
 import com.google.firebase.messaging.FirebaseMessagingException;
 import com.google.firebase.messaging.MulticastMessage;
 import com.google.firebase.messaging.Notification;
+
 import org.traccar.config.Config;
 import org.traccar.config.Keys;
 import org.traccar.model.Event;
@@ -40,6 +41,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
 
 @Singleton
 public class NotificatorFirebase implements Notificator {
@@ -67,7 +70,10 @@ public class NotificatorFirebase implements Notificator {
 
             var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
 
-            List<String> registrationTokens = Arrays.asList(user.getString("notificationTokens").split("[, ]"));
+            List<String> registrationTokens = Arrays.asList(user.getString("notificationTokens").split("[, ]"))
+                    .stream()
+                    .filter(t -> !t.isEmpty())
+                    .collect(Collectors.toList());
 
             MulticastMessage message = MulticastMessage.builder()
                     .setNotification(Notification.builder()
-- 
2.34.4
Anton Tananaev2 years ago

This should fix the problem properly:

https://github.com/traccar/traccar-web/commit/8dbfbcd6a5b7c9356c335debec28e70cd3952cb2

The cause was when you logout and it removed the last token, it was still leaving an empty attribute.

URLEY REYa year ago

Hi, I also have the same problem in version 5.5, but I don't get notifications to the mobile app. I have checked all the configuration several times and everything is fine, any current solution to the problem?

Anton Tananaeva year ago

You should probably create a separate thread and provide details like error messages etc.

jafar habibi4 months ago

I agree Iván Ávalos. the server should filter empty notification tokens. the issue persists if client uses custom client app or frontend.

Anton Tananaev4 months ago

Sounds like the client should fix their custom app or frontend.