post_enrollment_app/lib/pages/helpers/ecard_download_notification_service.dart

81 lines
2.3 KiB
Dart

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:open_filex/open_filex.dart';
import 'android_download_helper.dart';
class EcardDownloadNotificationService {
EcardDownloadNotificationService._();
static final FlutterLocalNotificationsPlugin _plugin =
FlutterLocalNotificationsPlugin();
static bool _initialized = false;
static Future<void> ensureInitialized() async {
if (_initialized || kIsWeb) return;
const androidSettings =
AndroidInitializationSettings('@mipmap/ic_launcher');
const iosSettings = DarwinInitializationSettings();
const settings = InitializationSettings(
android: androidSettings,
iOS: iosSettings,
);
await _plugin.initialize(
settings,
onDidReceiveNotificationResponse: (NotificationResponse response) async {
final filePath = response.payload;
if (filePath == null || filePath.isEmpty) return;
if (Platform.isAndroid) {
await openAndroidDownloadedFile(filePath);
return;
}
await OpenFilex.open(filePath);
},
);
await _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
await _plugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(alert: true, badge: true, sound: true);
_initialized = true;
}
static Future<void> showDownloadCompleted({
required String filePath,
required String fileName,
}) async {
if (kIsWeb) return;
await ensureInitialized();
const androidDetails = AndroidNotificationDetails(
'ecard_downloads_channel',
'E-Card Downloads',
channelDescription: 'Notifications for completed eCard downloads',
importance: Importance.max,
priority: Priority.high,
styleInformation: DefaultStyleInformation(true, true),
);
const iosDetails = DarwinNotificationDetails();
await _plugin.show(
DateTime.now().millisecondsSinceEpoch ~/ 1000,
'eCard downloaded',
'Tap to open $fileName',
const NotificationDetails(
android: androidDetails,
iOS: iosDetails,
),
payload: filePath,
);
}
}