57 lines
1.7 KiB
Dart
57 lines
1.7 KiB
Dart
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
/// A thin wrapper around `flutter_local_notifications` that centralizes the
|
|
/// plugin instance and provides easy initialization/show helpers.
|
|
class NotificationService {
|
|
NotificationService._();
|
|
|
|
static final FlutterLocalNotificationsPlugin _plugin =
|
|
FlutterLocalNotificationsPlugin();
|
|
|
|
/// Call during app startup, after any necessary permissions have been
|
|
/// granted. The callback receives a [NotificationResponse] when the user
|
|
/// taps the alert.
|
|
static Future<void> initialize({
|
|
void Function(NotificationResponse)? onDidReceiveNotificationResponse,
|
|
}) async {
|
|
const androidSettings = AndroidInitializationSettings(
|
|
'@mipmap/ic_launcher',
|
|
);
|
|
const iosSettings = DarwinInitializationSettings();
|
|
|
|
await _plugin.initialize(
|
|
settings: const InitializationSettings(
|
|
android: androidSettings,
|
|
iOS: iosSettings,
|
|
),
|
|
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
|
|
);
|
|
}
|
|
|
|
/// Show a simple notification immediately.
|
|
static Future<void> show({
|
|
required int id,
|
|
required String title,
|
|
required String body,
|
|
String? payload,
|
|
}) async {
|
|
const androidDetails = AndroidNotificationDetails(
|
|
'default_channel',
|
|
'General',
|
|
importance: Importance.high,
|
|
priority: Priority.high,
|
|
);
|
|
const iosDetails = DarwinNotificationDetails();
|
|
await _plugin.show(
|
|
id: id,
|
|
title: title,
|
|
body: body,
|
|
notificationDetails: const NotificationDetails(
|
|
android: androidDetails,
|
|
iOS: iosDetails,
|
|
),
|
|
payload: payload,
|
|
);
|
|
}
|
|
}
|