init method

Future<void> init()

Implementation

Future<void> init() async {
  log('Initializing notification service...');
  log('Requesting permission...');

  final settings = await FirebaseMessaging.instance.requestPermission();

  log('Permissions status: ${settings.authorizationStatus}');

  final isInit = await initializeLocalNotifications();
  if (!isInit) return;

  try {
    FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  } on ArgumentError catch (e) {
    log('Error setting background handler: $e');
  }

  if (!kIsWeb) {
    /// Update the iOS foreground notification presentation options to allow
    /// heads up notifications.
    if (observer.foregroundIOSNotificationEnabled) {
      await FirebaseMessaging.instance
          .setForegroundNotificationPresentationOptions(
        alert: observer.foregroundIOSNotificationAlertEnabled,
        badge: observer.foregroundIOSNotificationBadgeEnabled,
        sound: observer.foregroundIOSNotificationSoundEnabled,
      );
    }
    await setupNotifications();
  }

  /// Get the initial message if the app was opened from a terminated state
  final res = await FirebaseMessaging.instance.getInitialMessage();
  if (res != null) {
    log('Got initial message: $res');
    observer.messageOpenedAppController.add(res);
  }

  /// Listen for messages
  observer.onMessage.listen((message) {
    if (message.notification != null) {
      log('Message also contained a notification: ${message.notification}');
      showNotification(message);
    }
  });

  /// Initialize FCM token
  await _tokenService.init();
}