notificationTokenProvider top-level property

FutureProvider<String?> notificationTokenProvider
final

** Update notification token providers ** Provides the FCM notification token associated with the device

Implementation

/// Provides the FCM notification token associated with the device
final notificationTokenProvider = FutureProvider<String?>((ref) async {
  if (Platform.isIOS) {
    const maxRetries = 3;

    for (var retries = 0; retries < maxRetries; retries++) {
      try {
        // On iOS, we need to obtain an APNS token first before an FCM token
        final apnsToken = await FirebaseMessaging.instance.getAPNSToken();

        if (apnsToken != null) {
          return await FirebaseMessaging.instance.getToken();
        } else {
          // Add a delay, then retry
          await Future<void>.delayed(const Duration(seconds: 3));
        }
      } catch (e) {
        // Add a delay, then retry
        await Future<void>.delayed(const Duration(seconds: 3));
      }
    }

    return null;
  } else {
    return await FirebaseMessaging.instance.getToken();
  }
});