init method

Future<void> init({
  1. String? language,
  2. FirebaseOptions? firebaseOptions,
  3. void onResponse(
    1. String
    )?,
  4. Future<void> onBackground(
    1. RemoteMessage message
    )?,
})

Initializes this NotificationService.

Requests permission to send notifications if it hasn't been granted yet.

Optional onResponse callback is called when user taps on a notification.

Optional onBackground callback is called when a notification is received when the application is in the background or terminated. Note that this callback must be a top-level entry function, as it is launched in a background isolate.

Implementation

Future<void> init({
  String? language,
  FirebaseOptions? firebaseOptions,
  void Function(String)? onResponse,
  Future<void> Function(RemoteMessage message)? onBackground,
}) async {
  Log.debug(
    'init($language, firebaseOptions, onResponse, onBackground)',
    '$runtimeType',
  );

  if (WebUtils.isPopup) {
    return;
  }

  _language = language ?? _language;

  PlatformUtils.isActive.then((value) => _active = value);
  _onActivityChanged = PlatformUtils.onActivityChanged.listen(
    (v) => _active = v,
  );

  AudioUtils.ensureInitialized();

  _initLocalNotifications(
    onResponse: (NotificationResponse response) {
      if (response.payload != null) {
        onResponse?.call(response.payload!);
      }
    },
  );

  if (PlatformUtils.pushNotifications) {
    try {
      await _initPushNotifications(
        options: firebaseOptions,
        onResponse: (RemoteMessage message) {
          if (message.data['chatId'] != null) {
            onResponse?.call('${Routes.chats}/${message.data['chatId']}');
          }
        },
        onBackground: onBackground,
      );
    } catch (e) {
      Log.error(
        'Failed to initialize push notifications: ${e.toString()}',
        '$runtimeType',
      );
    }
  }
}