From 0249d9e99f2afc96fb8951b8aa080a090df71055 Mon Sep 17 00:00:00 2001 From: Nils Reichardt Date: Sun, 3 Sep 2023 12:29:22 +0200 Subject: [PATCH] Catch and retry if getting Firebase Messing token failed (#869) Fixes #868 --- app/lib/util/notification_token_adder.dart | 15 ++++++++++++++- app/pubspec.lock | 2 +- app/pubspec.yaml | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/lib/util/notification_token_adder.dart b/app/lib/util/notification_token_adder.dart index bcc689417..13121b6b9 100644 --- a/app/lib/util/notification_token_adder.dart +++ b/app/lib/util/notification_token_adder.dart @@ -10,6 +10,7 @@ import 'dart:developer'; import 'package:collection/collection.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; +import 'package:retry/retry.dart'; import 'package:sharezone/main/sharezone.dart'; import 'package:sharezone/util/api/user_api.dart'; @@ -59,7 +60,19 @@ class NotificationTokenAdderApi { return null; } - return _firebaseMessaging.getToken(vapidKey: vapidKey); + try { + // Retry because sometimes we get an unknown error from Firebase + // Messaging. + // + // See https://console.firebase.google.com/u/0/project/sharezone-c2bd8/crashlytics/app/ios:de.codingbrain.sharezone.app/issues/bd7d15551a69ad54ea806f4627d8eeda + return retry( + () => _firebaseMessaging.getToken(vapidKey: vapidKey), + maxAttempts: 3, + ); + } catch (e, s) { + log('Could not get FCM token', error: e, stackTrace: s); + return null; + } } Future tryAddTokenToDatabase(String token) async { diff --git a/app/pubspec.lock b/app/pubspec.lock index 57ab5c775..f46936bdf 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -1682,7 +1682,7 @@ packages: source: path version: "0.0.1" retry: - dependency: transitive + dependency: "direct main" description: name: retry sha256: "822e118d5b3aafed083109c72d5f484c6dc66707885e07c0fbcb8b986bba7efc" diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 3648da81b..2a476424f 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -132,6 +132,7 @@ dependencies: quiver: ^3.0.1 remote_configuration: path: ../lib/remote_configuration + retry: ^3.1.2 rxdart: ^0.27.1 scrollable_positioned_list: ^0.3.8 share: ^2.0.4