|
| 1 | +// Documents and Integration |
| 2 | +// https://pub.dev/packages/awesome_notifications |
| 3 | + |
| 4 | +import 'package:awesome_notifications/awesome_notifications.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +final notificationService = NotificationService(); |
| 8 | + |
| 9 | +class NotificationService { |
| 10 | + final _noti = AwesomeNotifications(); |
| 11 | + AwesomeNotifications get noti => _noti; |
| 12 | + |
| 13 | + void init() { |
| 14 | + _noti.initialize( |
| 15 | + null, |
| 16 | + [ |
| 17 | + NotificationChannel( |
| 18 | + channelKey: 'basic_channel', |
| 19 | + channelName: 'Basic Notifications', |
| 20 | + channelDescription: 'Notification Channel', |
| 21 | + defaultColor: Colors.teal, // primaryColor |
| 22 | + ledColor: Colors.white, |
| 23 | + channelShowBadge: true, |
| 24 | + importance: NotificationImportance.High, |
| 25 | + ), |
| 26 | + ], |
| 27 | + ); |
| 28 | + |
| 29 | + _noti.isNotificationAllowed().then((isAllowed) { |
| 30 | + // Edit |
| 31 | + if (!isAllowed) { |
| 32 | + _noti.requestPermissionToSendNotifications(); |
| 33 | + } else { |
| 34 | + listen(); |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + void listen() { |
| 40 | + // Edit |
| 41 | + _noti |
| 42 | + ..actionStream.listen((event) { |
| 43 | + print("[C_notiListenAction]: $event"); |
| 44 | + // if (event.buttonKeyPressed == "btn1") { |
| 45 | + // print("[C_btn1]: Button 1 Clicked"); |
| 46 | + // } else if (event.buttonKeyPressed == "btn2") { |
| 47 | + // print("[C_btn2]: Button 2 Clicked"); |
| 48 | + // } |
| 49 | + }) |
| 50 | + ..dismissedStream.listen((event) { |
| 51 | + print("[C_notiListenDismissed]: $event"); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + int create({ |
| 56 | + int? id, |
| 57 | + String? title, |
| 58 | + required String? body, |
| 59 | + String? groupKey, |
| 60 | + NotificationLayout? layout, |
| 61 | + String? largeIcon, |
| 62 | + String? bigPicture, |
| 63 | + Color? color, |
| 64 | + List<NotificationActionButton>? buttons, |
| 65 | + DateTime? showDate, |
| 66 | + }) { |
| 67 | + final _id = id ?? DateTime.now().millisecondsSinceEpoch.remainder(100000); |
| 68 | + _noti.createNotification( |
| 69 | + content: NotificationContent( |
| 70 | + channelKey: 'basic_channel', |
| 71 | + id: _id, |
| 72 | + title: title, |
| 73 | + body: body, |
| 74 | + groupKey: groupKey, |
| 75 | + color: color, |
| 76 | + largeIcon: largeIcon, |
| 77 | + bigPicture: bigPicture, |
| 78 | + notificationLayout: |
| 79 | + bigPicture != null ? NotificationLayout.BigPicture : layout, |
| 80 | + ), |
| 81 | + actionButtons: buttons, |
| 82 | + schedule: showDate == null |
| 83 | + ? null |
| 84 | + : NotificationCalendar.fromDate(date: showDate), |
| 85 | + ); |
| 86 | + return _id; |
| 87 | + } |
| 88 | +} |
0 commit comments