96 lines
2.8 KiB
Dart
96 lines
2.8 KiB
Dart
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||
|
import 'package:timezone/data/latest.dart' as tz;
|
||
|
import 'package:timezone/timezone.dart' as tz;
|
||
|
|
||
|
class NotificationService {
|
||
|
//NotificationService a singleton object
|
||
|
static final NotificationService _notificationService =
|
||
|
NotificationService._internal();
|
||
|
|
||
|
factory NotificationService() {
|
||
|
return _notificationService;
|
||
|
}
|
||
|
|
||
|
NotificationService._internal();
|
||
|
|
||
|
static const channelId = '123';
|
||
|
|
||
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
||
|
FlutterLocalNotificationsPlugin();
|
||
|
|
||
|
Future<void> init() async {
|
||
|
final AndroidInitializationSettings initializationSettingsAndroid =
|
||
|
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||
|
|
||
|
final IOSInitializationSettings initializationSettingsIOS =
|
||
|
IOSInitializationSettings(
|
||
|
requestSoundPermission: false,
|
||
|
requestBadgePermission: false,
|
||
|
requestAlertPermission: false,
|
||
|
);
|
||
|
|
||
|
final InitializationSettings initializationSettings =
|
||
|
InitializationSettings(
|
||
|
android: initializationSettingsAndroid,
|
||
|
iOS: initializationSettingsIOS,
|
||
|
macOS: null);
|
||
|
|
||
|
tz.initializeTimeZones();
|
||
|
|
||
|
await flutterLocalNotificationsPlugin.initialize(
|
||
|
initializationSettings,
|
||
|
onSelectNotification: (payload) {},
|
||
|
);
|
||
|
}
|
||
|
|
||
|
AndroidNotificationDetails _androidNotificationDetails =
|
||
|
// ignore: prefer_const_constructors
|
||
|
AndroidNotificationDetails(
|
||
|
'channel ID',
|
||
|
'channel name',
|
||
|
playSound: true,
|
||
|
priority: Priority.high,
|
||
|
importance: Importance.high,
|
||
|
);
|
||
|
|
||
|
Future<void> showNotifications(String title, String message) async {
|
||
|
await flutterLocalNotificationsPlugin.show(
|
||
|
0,
|
||
|
title,
|
||
|
message,
|
||
|
NotificationDetails(android: _androidNotificationDetails),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
void requestIOSPermissions() {
|
||
|
flutterLocalNotificationsPlugin
|
||
|
.resolvePlatformSpecificImplementation<
|
||
|
IOSFlutterLocalNotificationsPlugin>()
|
||
|
?.requestPermissions(
|
||
|
alert: true,
|
||
|
badge: true,
|
||
|
sound: true,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Future<void> scheduleNotifications() async {
|
||
|
await flutterLocalNotificationsPlugin.zonedSchedule(
|
||
|
0,
|
||
|
"Notification Title",
|
||
|
"This is the Notification Body!",
|
||
|
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 15)),
|
||
|
NotificationDetails(android: _androidNotificationDetails),
|
||
|
androidAllowWhileIdle: true,
|
||
|
uiLocalNotificationDateInterpretation:
|
||
|
UILocalNotificationDateInterpretation.absoluteTime);
|
||
|
}
|
||
|
|
||
|
Future<void> cancelNotifications(int id) async {
|
||
|
await flutterLocalNotificationsPlugin.cancel(id);
|
||
|
}
|
||
|
|
||
|
Future<void> cancelAllNotifications() async {
|
||
|
await flutterLocalNotificationsPlugin.cancelAll();
|
||
|
}
|
||
|
}
|