initialize static method

Future<void> initialize({
  1. dynamic notificationTap(
    1. Map<String, dynamic>
    )?,
})

Implementation

static Future<void> initialize({
  Function(Map<String, dynamic>)? notificationTap,
}) async {
  handleNotificationTap = notificationTap;
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('@mipmap/ic_launcher');

  final PackageInfo packageInfo = await PackageInfo.fromPlatform();
  InitializationSettings initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid,
    iOS: const DarwinInitializationSettings(),
    macOS: const DarwinInitializationSettings(),
    linux: const LinuxInitializationSettings(
      defaultActionName: 'Open notification',
    ),
    windows: WindowsInitializationSettings(
      appName: packageInfo.appName,
      appUserModelId: packageInfo.packageName,
      guid: 'a766a91c-e6ee-470f-88f9-cd21d7408541',
    ),
  );

  await flutterLocalNotificationsPlugin.initialize(
    onDidReceiveNotificationResponse: (NotificationResponse response) async {
      debugPrint('Foreground notification tapped: ${response.payload}');

      if (response.payload == null || (response.payload?.isEmpty ?? true)) {
        return;
      }

      await UFUtils.preferences.writeString(
        UFUtils.preferences.notificationPayload,
        jsonEncode(response.payload),
      );

      handleNotificationTap?.call(
        jsonDecode(makeValidJson(response.payload ?? "")),
      );
    },
    onDidReceiveBackgroundNotificationResponse:
        onDidReceiveBackgroundNotificationResponse,
    settings: initializationSettings,
  );

  if (_supportsFirebaseMessaging) {
    try {
      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        debugPrint('Received message: ${message.notification}');
        RemoteNotification? notification = message.notification;

        if (notification != null) {
          _showNotification(
            notification.title ?? "No Title",
            notification.body ?? "No Body",
            message.data,
          );
        }

        // handleNotificationTap?.call(jsonDecode(makeValidJson(message.data.toString())));
      });

      FirebaseMessaging.onBackgroundMessage(
        _firebaseMessagingBackgroundHandler,
      );
      await requestNotificationPermission();
    } catch (e) {
      debugPrint('Firebase Messaging not supported on this platform: $e');
    }
  }
}