@@ -21,7 +21,6 @@ import 'package:mobileraker/firebase_options.dart';
21
21
import 'package:mobileraker/service/printer_service.dart' ;
22
22
import 'package:mobileraker/ui/theme_setup.dart' ;
23
23
24
- import 'database_service.dart' ;
25
24
import 'machine_service.dart' ;
26
25
27
26
class NotificationService {
@@ -84,24 +83,24 @@ class NotificationService {
84
83
85
84
Future <void > initialize () async {
86
85
List <PrinterSetting > allMachines = await _machineService.fetchAll ();
87
- List <NotificationChannelGroup > groups = [];
88
- List <NotificationChannel > channels = [];
89
- for (PrinterSetting setting in allMachines) {
90
- groups.add (_channelGroupOfPrinterSettings (setting));
91
- channels.addAll (_channelsOfPrinterSettings (setting));
92
- _setupFCMOnPrinterOnceConnected (setting);
93
- }
94
86
95
- await setupNotificationChannels (groups, channels);
96
- await setupFirebaseMessaging ();
87
+ allMachines.forEach (_setupFCMOnPrinterOnceConnected);
88
+
89
+ await setupNotificationChannels (allMachines);
97
90
98
91
for (PrinterSetting setting in allMachines) {
99
- _printerStreamMap[setting.uuid] = setting.printerService.printerStream
100
- .listen ((value) => _processPrinterUpdate (setting, value));
92
+ registerLocalMessageHandling (setting);
101
93
}
102
94
103
95
_hiveStreamListener = setupHiveBoxListener ();
104
96
_actionStreamListener = setupNotificationActionListener ();
97
+ await setupFirebaseMessaging ();
98
+ }
99
+
100
+ void registerLocalMessageHandling (PrinterSetting setting) {
101
+ if (Platform .isIOS) return ;
102
+ _printerStreamMap[setting.uuid] = setting.printerService.printerStream
103
+ .listen ((value) => _processPrinterUpdate (setting, value));
105
104
}
106
105
107
106
StreamSubscription <ReceivedAction > setupNotificationActionListener () {
@@ -122,13 +121,19 @@ class NotificationService {
122
121
});
123
122
}
124
123
125
- setupNotificationChannels (List <NotificationChannelGroup > printerNotifyGrp,
126
- List <NotificationChannel > printerNotifyChan) async {
124
+ setupNotificationChannels (List <PrinterSetting > machines) async {
125
+ List <NotificationChannelGroup > groups = [];
126
+ List <NotificationChannel > channels = [];
127
+ for (PrinterSetting setting in machines) {
128
+ groups.add (_channelGroupOfPrinterSettings (setting));
129
+ channels.addAll (_channelsOfPrinterSettings (setting));
130
+ }
131
+
127
132
await AwesomeNotifications ().initialize (
128
133
// set the icon to null if you want to use the default app icon
129
134
null ,
130
- printerNotifyChan ,
131
- channelGroups: printerNotifyGrp );
135
+ channels ,
136
+ channelGroups: groups );
132
137
133
138
await AwesomeNotifications ().isNotificationAllowed ().then ((isAllowed) {
134
139
if (! isAllowed) {
@@ -175,8 +180,7 @@ class NotificationService {
175
180
_channelsOfPrinterSettings (setting);
176
181
channelsOfPrinterSettings.forEach ((e) => _notifyAPI.setChannel (e));
177
182
_setupFCMOnPrinterOnceConnected (setting);
178
- _printerStreamMap[setting.uuid] = setting.printerService.printerStream
179
- .listen ((value) => _processPrinterUpdate (setting, value));
183
+ registerLocalMessageHandling (setting);
180
184
_logger.i (
181
185
"Added notifications channels and stream-listener for UUID=${setting .uuid }" );
182
186
}
@@ -189,35 +193,6 @@ class NotificationService {
189
193
.i ("Removed notifications channels and stream-listener for UUID=$uuid " );
190
194
}
191
195
192
- /// Register the FCM token of the device in the moonraker database to ensure
193
- /// that our python companion knows the notification targets!
194
- Future <String > _registerFCMTokenOnMachine (
195
- PrinterSetting printerSetting) async {
196
- DatabaseService databaseService = printerSetting.databaseService;
197
- String ? fcmToken = await FirebaseMessaging .instance.getToken ();
198
- if (fcmToken == null ) {
199
- _logger.w ("Could not fetch fcm token" );
200
- return Future .error ("No token available for device!" );
201
- }
202
-
203
- var item =
204
- await databaseService.getDatabaseItem ('mobileraker' , 'fcmTokens' );
205
- if (item == null ) {
206
- _logger.i ("Creating fcmTokens in moonraker-Database" );
207
- await databaseService
208
- .addDatabaseItem ('mobileraker' , 'fcmTokens' , [fcmToken]);
209
- } else {
210
- List <String > fcmTokens = List .from (item);
211
- if (! fcmTokens.contains (fcmToken)) {
212
- _logger.i ("Adding token to existing fcmTokens in moonraker-Database" );
213
- await databaseService.addDatabaseItem (
214
- 'mobileraker' , 'fcmTokens' , fcmTokens..add (fcmToken));
215
- }
216
- }
217
-
218
- return fcmToken;
219
- }
220
-
221
196
List <NotificationChannel > _channelsOfPrinterSettings (
222
197
PrinterSetting printerSetting) {
223
198
return [
@@ -236,6 +211,7 @@ class NotificationService {
236
211
playSound: false ,
237
212
enableVibration: false ,
238
213
enableLights: false ,
214
+ importance: NotificationImportance .Low ,
239
215
defaultColor: brownish.shade500)
240
216
];
241
217
}
@@ -250,16 +226,25 @@ class NotificationService {
250
226
Future <void > _setupFCMOnPrinterOnceConnected (PrinterSetting setting) async {
251
227
await setting.websocket.stateStream
252
228
.firstWhere ((element) => element == WebSocketState .connected);
229
+
230
+ String ? fcmToken = await FirebaseMessaging .instance.getToken ();
231
+ if (fcmToken == null ) {
232
+ _logger.w ("Could not fetch fcm token" );
233
+ return Future .error ("No token available for device!" );
234
+ }
235
+
253
236
_machineService.fetchOrCreateFcmIdentifier (setting);
254
- _registerFCMTokenOnMachine (setting);
237
+ _machineService.registerFCMTokenOnMachine (setting, fcmToken);
238
+
239
+ // _machineService.registerFCMTokenOnMachineNEW(setting, fcmToken);
255
240
}
256
241
257
242
Future <void > _processPrinterUpdate (
258
243
PrinterSetting printerSetting, Printer printer) async {
259
244
var state = await _updatePrintStatusNotification (
260
245
printerSetting, printer.print.state, printer.print.filename);
261
246
262
- if (state == PrintState .printing)
247
+ if (state == PrintState .printing && ! Platform .isIOS )
263
248
_updatePrintProgressNotification (printerSetting,
264
249
printer.virtualSdCard.progress, printer.print.printDuration);
265
250
await printerSetting.save ();
0 commit comments