-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
notification_service.dart
167 lines (148 loc) · 6.21 KB
/
notification_service.dart
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
import 'package:enum_to_string/enum_to_string.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/services/logging_service.dart';
import 'package:genshindb/domain/services/notification_service.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
const _channelId = 'genshindb_channel';
const _channelName = 'Notifications';
const _channelDescription = 'Notifications from the app';
const _largeIcon = 'genshin_db';
//Here we use this one in particular cause this tz uses UTC and does not use any kind of dst.
const _fallbackTimeZone = 'Africa/Accra';
class NotificationServiceImpl implements NotificationService {
final LoggingService _loggingService;
final _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
tz.Location _location;
NotificationServiceImpl(this._loggingService);
@override
Future<void> init() async {
try {
tz.initializeTimeZones();
final currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
_location = tz.getLocation(currentTimeZone) ?? tz.local;
tz.setLocalLocation(_location);
} catch (e, s) {
//https://github.com/srawlins/timezone/issues/92
_loggingService.error(runtimeType, 'init: Failed to get timezone or device is GMT or UTC, assigning the generic one...', e, s);
_location = tz.getLocation(_fallbackTimeZone);
assert(_location != null);
tz.setLocalLocation(_location);
}
}
@override
Future<void> registerCallBacks({
DidReceiveLocalNotificationCallback onIosReceiveLocalNotification,
SelectNotificationCallback onSelectNotification,
}) async {
try {
const initializationSettingsAndroid = AndroidInitializationSettings('ic_notification');
final initializationSettingsIOS = IOSInitializationSettings(onDidReceiveLocalNotification: onIosReceiveLocalNotification);
final initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
await _flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
} catch (e, s) {
_loggingService.error(runtimeType, 'registerCallBacks: Unknown error occurred', e, s);
}
}
@override
Future<bool> requestIOSPermissions() async {
final specificImpl = _flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>();
final result = await specificImpl?.requestPermissions(alert: true, badge: true, sound: true);
if (result == null) {
return false;
}
return result;
}
@override
Future<void> showNotification(int id, AppNotificationType type, String title, String body, {String payload}) {
final specifics = _getPlatformChannelSpecifics(type, body);
final newId = _generateUniqueId(id, type);
return _flutterLocalNotificationsPlugin.show(newId, title, body, specifics, payload: payload);
}
@override
Future<void> cancelNotification(int id, AppNotificationType type) {
return _flutterLocalNotificationsPlugin.cancel(id, tag: _getTagFromNotificationType(type));
}
@override
Future<void> cancelAllNotifications() {
return _flutterLocalNotificationsPlugin.cancelAll();
}
@override
Future<void> scheduleNotification(int id, AppNotificationType type, String title, String body, DateTime toBeDeliveredOn) async {
final now = DateTime.now();
final payload = '${id}_${_getTagFromNotificationType(type)}';
if (toBeDeliveredOn.isBefore(now) || toBeDeliveredOn.isAtSameMomentAs(now)) {
await showNotification(id, type, title, body, payload: payload);
return;
}
assert(_location != null);
final newId = _generateUniqueId(id, type);
final specifics = _getPlatformChannelSpecifics(type, body);
final scheduledDate = tz.TZDateTime.from(toBeDeliveredOn, _location);
return _flutterLocalNotificationsPlugin.zonedSchedule(
newId,
title,
body,
scheduledDate,
specifics,
uiLocalNotificationDateInterpretation: null,
androidAllowWhileIdle: true,
payload: payload,
);
}
NotificationDetails _getPlatformChannelSpecifics(AppNotificationType type, String body) {
final style = body.length < 40 ? null : BigTextStyleInformation(body);
final _androidPlatformChannelSpecifics = AndroidNotificationDetails(
_channelId,
_channelName,
_channelDescription,
importance: Importance.max,
priority: Priority.high,
enableLights: true,
color: Colors.red,
styleInformation: style,
largeIcon: const DrawableResourceAndroidBitmap(_largeIcon),
tag: _getTagFromNotificationType(type),
);
const _iOSPlatformChannelSpecifics = IOSNotificationDetails();
return NotificationDetails(android: _androidPlatformChannelSpecifics, iOS: _iOSPlatformChannelSpecifics);
}
String _getTagFromNotificationType(AppNotificationType type) {
return EnumToString.convertToString(type);
}
// For some reason I need to provide a unique id even if I'm providing a custom tag
// That's why we generate this id here
int _generateUniqueId(int id, AppNotificationType type) {
final prefix = _getIdPrefix(type).toString();
final newId = '$prefix$id';
return int.parse(newId);
}
int _getIdPrefix(AppNotificationType type) {
switch (type) {
case AppNotificationType.resin:
return 10;
case AppNotificationType.expedition:
return 20;
case AppNotificationType.farmingMaterials:
return 30;
case AppNotificationType.farmingArtifacts:
return 40;
case AppNotificationType.gadget:
return 50;
case AppNotificationType.furniture:
return 60;
case AppNotificationType.realmCurrency:
return 70;
case AppNotificationType.weeklyBoss:
return 80;
case AppNotificationType.custom:
return 90;
case AppNotificationType.dailyCheckIn:
return 100;
}
throw Exception('The provided type = $type is not a valid notification type');
}
}