|
| 1 | +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; |
| 2 | + |
| 3 | +class LocalNotificationService { |
| 4 | + final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = |
| 5 | + FlutterLocalNotificationsPlugin(); |
| 6 | + |
| 7 | + final AndroidInitializationSettings androidInitializationSettings = |
| 8 | + const AndroidInitializationSettings('@mipmap/ic_launcher'); |
| 9 | + |
| 10 | + void initializeNotification() async { |
| 11 | + InitializationSettings initializationSettings = |
| 12 | + InitializationSettings(android: androidInitializationSettings); |
| 13 | + |
| 14 | + await flutterLocalNotificationsPlugin.initialize(initializationSettings); |
| 15 | + } |
| 16 | + |
| 17 | + void showNotification(String title, String body) async { |
| 18 | + AndroidNotificationDetails androidNotificationDetails = |
| 19 | + const AndroidNotificationDetails( |
| 20 | + 'channelId', |
| 21 | + 'channelName', |
| 22 | + importance: Importance.max, |
| 23 | + priority: Priority.high, |
| 24 | + ); |
| 25 | + |
| 26 | + NotificationDetails notificationDetails = NotificationDetails( |
| 27 | + android: androidNotificationDetails, |
| 28 | + ); |
| 29 | + |
| 30 | + await flutterLocalNotificationsPlugin.show( |
| 31 | + 0, title, body, notificationDetails); |
| 32 | + } |
| 33 | + |
| 34 | + void scheduleNotification(String title, String body) async { |
| 35 | + AndroidNotificationDetails androidNotificationDetails = |
| 36 | + const AndroidNotificationDetails( |
| 37 | + 'channelId', |
| 38 | + 'channelName', |
| 39 | + importance: Importance.max, |
| 40 | + priority: Priority.high, |
| 41 | + ); |
| 42 | + |
| 43 | + NotificationDetails notificationDetails = NotificationDetails( |
| 44 | + android: androidNotificationDetails, |
| 45 | + ); |
| 46 | + |
| 47 | + await flutterLocalNotificationsPlugin.periodicallyShow( |
| 48 | + 0, |
| 49 | + title, |
| 50 | + body, |
| 51 | + RepeatInterval.everyMinute, |
| 52 | + notificationDetails, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + void stopScheduleNotification(id) async { |
| 57 | + await flutterLocalNotificationsPlugin.cancel(id); |
| 58 | + } |
| 59 | + |
| 60 | + void stopAllScheduleNotification() async { |
| 61 | + await flutterLocalNotificationsPlugin.cancelAll(); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +} |
0 commit comments