init method

Future<void> init()

Implementation

Future<void> init() async {
  try {
    Logger.printMessage('Getting APNS token...');
    if (!kIsWeb && (Platform.isIOS || Platform.isMacOS)) {
      final apnsToken = await FirebaseMessaging.instance.getAPNSToken();
      if (apnsToken == null) {
        throw Exception('APNS token is null');
      }
    }
    Logger.printMessage('Getting FCM token...');
    final res = await FirebaseMessaging.instance
        .getToken()
        .timeout(const Duration(seconds: 2), onTimeout: () {
      print('timeout');
      return null;
    });
    _setToken(res);
    PushNotificationsObserver.instance.tokenController.add(res);
    FirebaseMessaging.instance.onTokenRefresh
        .listen(PushNotificationsObserver.instance.tokenController.add);
  } catch (e) {
    Logger.printError('Error getting FCM token: $e');
  }
}