-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathnotificationService.dart
More file actions
195 lines (184 loc) · 6.23 KB
/
Copy pathnotificationService.dart
File metadata and controls
195 lines (184 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/material.dart';
class NotificationService {
static Future<void> initialNotification() async {
await AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelGroupKey: 'high_importance_channel',
channelKey: 'high_importance_channel',
channelName: 'Basic Notification',
channelDescription: 'Notification channel for basic tests',
defaultColor: Color(0xFF9D50DD),
importance: NotificationImportance.High,
channelShowBadge: true,
playSound: true,
onlyAlertOnce: true,
criticalAlerts: true,
ledColor: Colors.white,
)
],
channelGroups: [
NotificationChannelGroup(
channelGroupKey: 'high_importance_channel_group',
channelGroupName: 'Grouped 1',
)
],
debug: true,
);
// bool isAllowed = await AwesomeNotifications().isNotificationAllowed();
// if (!isAllowed) {
// await AwesomeNotifications().requestPermissionToSendNotifications();
// }
await AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
onNotificationCreatedMethod: onNotificationCreatedMethod,
onNotificationDisplayedMethod: onNotificationDisplayedMethod,
onDismissActionReceivedMethod: onDismissActionReceivedMethod,
);
}
@pragma("vm:entry-point")
static Future<void> onNotificationCreatedMethod(
ReceivedNotification receivedNotification) async {
debugPrint('notification created: ${receivedNotification.title}');
}
@pragma("vm:entry-point")
static Future<void> onNotificationDisplayedMethod(
ReceivedNotification receivedNotification) async {
debugPrint('notification displayed: ${receivedNotification.title}');
}
@pragma("vm:entry-point")
static Future<void> onDismissActionReceivedMethod(
ReceivedAction receivedAction) async {
debugPrint('notification dismissed: ${receivedAction.id}');
}
@pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
debugPrint('notification action received: ${receivedAction.id}');
final payload = receivedAction.payload;
if (payload != null) {
debugPrint('Payload: $payload');
}
}
static Future<bool> showNotification({
required final String title,
required final String body,
final String? summary,
final Map<String, String>? payload,
final ActionType actionType = ActionType.Default,
final NotificationLayout notificationLayout = NotificationLayout.Default,
final NotificationCategory? category,
final String? bigPicture,
final List<NotificationActionButton>? actionButtons,
final bool scheduled = false,
final int? interval,
}) async {
assert(!scheduled || (scheduled && interval != null));
bool allowed =
await AwesomeNotifications().requestPermissionToSendNotifications();
if (allowed) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: -1,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: scheduled
? NotificationInterval(
interval: interval != null ? Duration(seconds: interval) : null,
timeZone:
await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true,
)
: null,
);
} else {
allowed = await AwesomeNotifications().isNotificationAllowed();
if (allowed) {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: -1,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: scheduled
? NotificationInterval(
interval: interval != null ? Duration(seconds: interval) : null,
timeZone:
await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true,
)
: null,
);
}
}
return allowed;
}
static Future<void> scheduleNotificationsForEvent(
String description, DateTime startDate, DateTime endDate) async {
await _scheduleNotification(
description,
'Application Period starts in one week!',
startDate.subtract(const Duration(days: 7)),
);
await _scheduleNotification(
description,
'Application Period starts tomorrow!',
startDate.subtract(const Duration(days: 1)),
);
await _scheduleNotification(
description,
'Application Period ends in one week!',
endDate.subtract(const Duration(days: 7)),
);
await _scheduleNotification(
description,
'Application Period ends tomorrow!',
endDate.subtract(const Duration(days: 1)),
);
}
static Future<bool> _scheduleNotification(
String description, String body, DateTime dateTime) async {
bool allowed = await AwesomeNotifications().isNotificationAllowed();
if (allowed) {
int uniqueId = _generateDeterministicId(description, body);
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: uniqueId,
channelKey: 'high_importance_channel',
title: description,
body: body,
notificationLayout: NotificationLayout.Default,
),
schedule: NotificationCalendar.fromDate(date: dateTime),
);
}
return allowed;
}
static int _generateDeterministicId(String title, String body) {
String key = "$title|$body";
int hash = 0;
for (int i = 0; i < key.length; i++) {
hash = (hash * 31 + key.codeUnitAt(i)) & 0x7FFFFFFF;
}
return hash;
}
}