-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathflutter_foreground_task_method_channel.dart
273 lines (237 loc) · 7.8 KB
/
flutter_foreground_task_method_channel.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import 'dart:async';
import 'dart:convert';
import 'dart:developer' as dev;
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:platform/platform.dart';
import 'flutter_foreground_task_platform_interface.dart';
import 'models/foreground_task_options.dart';
import 'models/notification_button.dart';
import 'models/notification_icon.dart';
import 'models/notification_options.dart';
import 'models/notification_permission.dart';
import 'models/service_options.dart';
import 'task_handler.dart';
/// An implementation of [FlutterForegroundTaskPlatform] that uses method channels.
class MethodChannelFlutterForegroundTask extends FlutterForegroundTaskPlatform {
@visibleForTesting
final MethodChannel mMDChannel =
const MethodChannel('flutter_foreground_task/methods');
@visibleForTesting
final MethodChannel mBGChannel =
const MethodChannel('flutter_foreground_task/background');
@visibleForTesting
Platform platform = const LocalPlatform();
// ====================== Service ======================
@override
Future<void> startService({
required AndroidNotificationOptions androidNotificationOptions,
required IOSNotificationOptions iosNotificationOptions,
required ForegroundTaskOptions foregroundTaskOptions,
int? serviceId,
required String notificationTitle,
required String notificationText,
NotificationIcon? notificationIcon,
List<NotificationButton>? notificationButtons,
String? notificationInitialRoute,
Function? callback,
}) async {
final Map<String, dynamic> optionsJson = ServiceStartOptions(
serviceId: serviceId,
androidNotificationOptions: androidNotificationOptions,
iosNotificationOptions: iosNotificationOptions,
foregroundTaskOptions: foregroundTaskOptions,
notificationContentTitle: notificationTitle,
notificationContentText: notificationText,
notificationIcon: notificationIcon,
notificationButtons: notificationButtons,
notificationInitialRoute: notificationInitialRoute,
callback: callback,
).toJson(platform);
await mMDChannel.invokeMethod('startService', optionsJson);
}
@override
Future<void> restartService() async {
await mMDChannel.invokeMethod('restartService');
}
@override
Future<void> updateService({
ForegroundTaskOptions? foregroundTaskOptions,
String? notificationTitle,
String? notificationText,
NotificationIcon? notificationIcon,
List<NotificationButton>? notificationButtons,
String? notificationInitialRoute,
Function? callback,
}) async {
final Map<String, dynamic> optionsJson = ServiceUpdateOptions(
foregroundTaskOptions: foregroundTaskOptions,
notificationContentTitle: notificationTitle,
notificationContentText: notificationText,
notificationIcon: notificationIcon,
notificationButtons: notificationButtons,
notificationInitialRoute: notificationInitialRoute,
callback: callback,
).toJson(platform);
await mMDChannel.invokeMethod('updateService', optionsJson);
}
@override
Future<void> stopService() async {
await mMDChannel.invokeMethod('stopService');
}
@override
Future<bool> get isRunningService async {
return await mMDChannel.invokeMethod('isRunningService');
}
@override
Future<bool> get attachedActivity async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('attachedActivity');
}
return true;
}
@override
void setTaskHandler(TaskHandler handler) {
// Binding the framework to the flutter engine.
WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();
// Set the method call handler for the background channel.
mBGChannel.setMethodCallHandler((call) async {
await onBackgroundChannel(call, handler);
});
mBGChannel.invokeMethod('start');
}
@visibleForTesting
Future<void> onBackgroundChannel(MethodCall call, TaskHandler handler) async {
final DateTime timestamp = DateTime.timestamp();
switch (call.method) {
case 'onStart':
final TaskStarter starter = TaskStarter.fromIndex(call.arguments);
await handler.onStart(timestamp, starter);
break;
case 'onRepeatEvent':
handler.onRepeatEvent(timestamp);
break;
case 'onDestroy':
final bool isTimeout = call.arguments ?? false;
await handler.onDestroy(timestamp, isTimeout);
break;
case 'onReceiveData':
dynamic data = call.arguments;
if (data is List || data is Map || data is Set) {
try {
data = jsonDecode(jsonEncode(data));
} catch (e, s) {
dev.log('onReceiveData error: $e\n$s');
}
}
handler.onReceiveData(data);
break;
case 'onNotificationButtonPressed':
final String id = call.arguments.toString();
handler.onNotificationButtonPressed(id);
break;
case 'onNotificationDismissed':
handler.onNotificationDismissed();
break;
case 'onNotificationPressed':
handler.onNotificationPressed();
break;
}
}
// =================== Communication ===================
@override
void sendDataToTask(Object data) {
mMDChannel.invokeMethod('sendData', data);
}
// ====================== Utility ======================
@override
void minimizeApp() {
mMDChannel.invokeMethod('minimizeApp');
}
@override
void launchApp([String? route]) {
if (platform.isAndroid) {
mMDChannel.invokeMethod('launchApp', route);
}
}
@override
void setOnLockScreenVisibility(bool isVisible) {
if (platform.isAndroid) {
mMDChannel.invokeMethod('setOnLockScreenVisibility', isVisible);
}
}
@override
Future<bool> get isAppOnForeground async {
return await mMDChannel.invokeMethod('isAppOnForeground');
}
@override
void wakeUpScreen() {
if (platform.isAndroid) {
mMDChannel.invokeMethod('wakeUpScreen');
}
}
@override
Future<bool> get isIgnoringBatteryOptimizations async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('isIgnoringBatteryOptimizations');
}
return true;
}
@override
Future<bool> openIgnoreBatteryOptimizationSettings() async {
if (platform.isAndroid) {
return await mMDChannel
.invokeMethod('openIgnoreBatteryOptimizationSettings');
}
return true;
}
@override
Future<bool> requestIgnoreBatteryOptimization() async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('requestIgnoreBatteryOptimization');
}
return true;
}
@override
Future<bool> get canDrawOverlays async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('canDrawOverlays');
}
return true;
}
@override
Future<bool> openSystemAlertWindowSettings() async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('openSystemAlertWindowSettings');
}
return true;
}
@override
Future<NotificationPermission> checkNotificationPermission() async {
final int result =
await mMDChannel.invokeMethod('checkNotificationPermission');
return NotificationPermission.fromIndex(result);
}
@override
Future<NotificationPermission> requestNotificationPermission() async {
final int result =
await mMDChannel.invokeMethod('requestNotificationPermission');
return NotificationPermission.fromIndex(result);
}
@override
Future<bool> get canScheduleExactAlarms async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('canScheduleExactAlarms');
}
return true;
}
@override
Future<bool> openAlarmsAndRemindersSettings() async {
if (platform.isAndroid) {
return await mMDChannel.invokeMethod('openAlarmsAndRemindersSettings');
}
return true;
}
}