Saba-dart/lib/utils/notification_helper.dart
2026-04-13 23:41:27 +03:30

68 lines
2.1 KiB
Dart

import 'dart:async';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class NotificationHelper {
static final NotificationHelper instance = NotificationHelper._init();
final StreamController<String> notificationStreamController = StreamController<String>.broadcast();
Stream<String> get notificationStream => notificationStreamController.stream;
final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
bool _isInitialized = false;
NotificationHelper._init();
Future<void> init() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
const InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
);
await _notificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (details) {
if (details.payload != null) {
// This will be handled by the UI listening to a stream or similar
// or we can use a global navigator key if available.
// For now, we'll use a static notificationStream to let listeners know.
notificationStreamController.add(details.payload!);
}
},
);
_isInitialized = true;
}
Future<void> showNotification({
required int id,
required String title,
required String body,
String? payload,
}) async {
if (!_isInitialized) await init();
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'sms_channel',
'SMS Notifications',
channelDescription: 'Notifications for incoming secure messages',
importance: Importance.max,
priority: Priority.high,
showWhen: true,
);
const NotificationDetails platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
);
await _notificationsPlugin.show(
id,
title,
body,
platformChannelSpecifics,
payload: payload,
);
}
}