forked from Raja0sama/rn-foreground-service
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
406 lines (368 loc) · 11.1 KB
/
index.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { NativeModules, AppRegistry, DeviceEventEmitter } from "react-native";
// ANDROID ONLY
// Copied and adapted from https://github.com/voximplant/react-native-foreground-service
// and https://github.com/zo0r/react-native-push-notification/
const ForegroundServiceModule = NativeModules.ForegroundService;
/**
* @property {number} id - Unique notification id
* @property {string} title - Notification title
* @property {string} message - Notification message
* @property {string} ServiceType - Foreground service types are Mandatory in Android 14
* @property {string} number - int specified as string > 0, for devices that support it, this might be used to set the badge counter
* @property {string} icon - Small icon name | ic_notification
* @property {string} largeIcon - Large icon name | ic_launcher
* @property {string} visibility - private | public | secret
* @property {boolean} ongoing - true/false if the notification is ongoing. The notification the service was started with will always be ongoing
* @property {number} [importance] - Importance (and priority for older devices) of this notification. This might affect notification sound One of:
* none - IMPORTANCE_NONE (by default),
* min - IMPORTANCE_MIN,
* low - IMPORTANCE_LOW,
* default - IMPORTANCE_DEFAULT
* high - IMPORTANCE_HIGH,
* max - IMPORTANCE_MAX
*/
const NotificationConfig = {};
/**
* @property {string} taskName - name of the js task configured with registerForegroundTask
* @property {number} delay - start task in delay miliseconds, use 0 to start immediately
* ... any other values passed to the task as well
*/
const TaskConfig = {};
class ForegroundService {
/**
* Registers a piece of JS code to be ran on the service
* NOTE: This must be called before anything else, or the service will fail.
* NOTE2: Registration must also happen at module level (not at mount)
* task will receive all parameters from runTask
* @param {task} async function to be called
*/
static registerForegroundTask(taskName, task) {
AppRegistry.registerHeadlessTask(taskName, () => task);
}
/**
* Start foreground service
* Multiple calls won't start multiple instances of the service, but will increase its internal counter
* so calls to stop won't stop until it reaches 0.
* Note: notificationConfig can't be re-used (becomes immutable)
* @param {NotificationConfig} notificationConfig - Notification config
* @return Promise
*/
static async startService(notificationConfig) {
console.log("Start Service Triggered");
return await ForegroundServiceModule.startService(notificationConfig);
}
/**
* Updates a notification of a running service. Make sure to use the same ID
* or it will trigger a separate notification.
* Note: this method might fail if called right after starting the service
* since the service might not be yet ready.
* If service is not running, it will be started automatically like calling startService.
* @param {NotificationConfig} notificationConfig - Notification config
* @return Promise
*/
static async updateNotification(notificationConfig) {
console.log(" Update Service Triggered");
return await ForegroundServiceModule.updateNotification(notificationConfig);
}
/**
* Cancels/dimisses a notification given its id. Useful if the service used
* more than one notification
* @param {number} id - Notification id to cancel
* @return Promise
*/
static async cancelNotification(id) {
console.log("Cancel Service Triggered");
return await ForegroundServiceModule.cancelNotification({ id: id });
}
/**
* Stop foreground service. Note: Pending tasks might still complete.
* If startService will called multiple times, this needs to be called as many times.
* @return Promise
*/
static async stopService() {
console.log("Stop Service Triggered");
return await ForegroundServiceModule.stopService();
}
/**
* Stop foreground service. Note: Pending tasks might still complete.
* This will stop the service regardless of how many times start was called
* @return Promise
*/
static async stopServiceAll() {
return await ForegroundServiceModule.stopServiceAll();
}
/**
* Runs a previously configured headless task.
* Task must be able to self stop if the service is stopped, since it can't be force killed once started.
* Note: This method might silently fail if the service is not running, but will run successfully
* if the service is still spinning up.
* If the service is not running because it was killed, it will be attempted to be started again
* using the last notification available.
* @param {TaskConfig} taskConfig - Notification config
* @return Promise
*/
static async runTask(taskConfig) {
return await ForegroundServiceModule.runTask(taskConfig);
}
/**
* Returns an integer indicating if the service is running or not.
* The integer represents the internal counter of how many startService
* calls were done without calling stopService
* @return Promise
*/
static async isRunning() {
return await ForegroundServiceModule.isRunning();
}
}
const randHashString = (len) => {
return "x".repeat(len).replace(/[xy]/g, (c) => {
let r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
//initial state
let tasks = {};
const samplingInterval = 500; //ms
let serviceRunning = false;
const deleteTask = (taskId) => {
delete tasks[taskId];
};
const taskRunner = async () => {
try {
if (!serviceRunning) return;
const now = Date.now();
let promises = [];
//iterate over all tasks
Object.entries(tasks).forEach(([taskId, task]) => {
//check if this task's execution time has arrived
if (now >= task.nextExecutionTime) {
//push this task's promise for later execution
promises.push(
Promise.resolve(task.task()).then(task.onSuccess, task.onError)
);
//if this is a looped task then increment its nextExecutionTime by delay for the next interval
if (task.onLoop) task.nextExecutionTime = now + task.delay;
//else delete the one-off task
else deleteTask(taskId);
}
});
//execute all tasks promises in parallel
await Promise.all(promises);
} catch (error) {
console.log("Error in FgService taskRunner:", error);
}
};
const register = () => {
if (!serviceRunning)
return ForegroundService.registerForegroundTask("myTaskName", taskRunner);
};
const start = async ({
id,
title = id,
message = "Foreground Service Running...",
ServiceType,
vibration = false,
visibility = "public",
icon = "ic_notification",
largeIcon = "ic_launcher",
importance = "max",
number = "1",
button = false,
buttonText = "",
buttonOnPress = "buttonOnPress",
button2 = false,
button2Text = "",
button2OnPress = "button2OnPress",
mainOnPress = "mainOnPress",
progress,
color,
setOnlyAlertOnce,
}) => {
try {
if (!serviceRunning) {
await ForegroundService.startService({
id,
title,
message,
ServiceType,
vibration,
visibility,
icon,
largeIcon,
importance,
number,
button,
buttonText,
buttonOnPress,
button2,
button2Text,
button2OnPress,
mainOnPress,
progressBar: !!progress,
progressBarMax: progress?.max,
progressBarCurr: progress?.curr,
color,
setOnlyAlertOnce,
});
serviceRunning = true;
await ForegroundService.runTask({
taskName: "myTaskName",
delay: samplingInterval,
loopDelay: samplingInterval,
onLoop: true,
});
} else console.log("Foreground service is already running.");
} catch (error) {
throw error;
}
};
const update = async ({
id,
title = id,
message = "Foreground Service Running...",
ServiceType,
vibration = false,
visibility = "public",
largeIcon = "ic_launcher",
icon = "ic_launcher",
importance = "max",
number = "0",
button = false,
buttonText = "",
buttonOnPress = "buttonOnPress",
button2 = false,
button2Text = "",
button2OnPress = "button2OnPress",
mainOnPress = "mainOnPress",
progress,
color,
setOnlyAlertOnce,
}) => {
try {
await ForegroundService.updateNotification({
id,
title,
message,
ServiceType,
vibration,
visibility,
largeIcon,
icon,
importance,
number,
button,
buttonText,
buttonOnPress,
button2,
button2Text,
button2OnPress,
mainOnPress,
progressBar: !!progress,
progressBarMax: progress?.max,
progressBarCurr: progress?.curr,
setOnlyAlertOnce,
color,
});
if (!serviceRunning) {
serviceRunning = true;
await ForegroundService.runTask({
taskName: "myTaskName",
delay: samplingInterval,
loopDelay: samplingInterval,
onLoop: true,
});
}
} catch (error) {
throw error;
}
};
const stop = () => {
serviceRunning = false;
return ForegroundService.stopService();
};
const stopAll = () => {
serviceRunning = false;
return ForegroundService.stopServiceAll();
};
const is_running = () => serviceRunning;
const add_task = (
task,
{
delay = 5000,
onLoop = true,
taskId = randHashString(12),
onSuccess = () => {},
onError = () => {},
}
) => {
const _type = typeof task;
if (_type !== "function")
throw `invalid task of type ${_type}, expected a function or a Promise`;
if (!tasks[taskId])
tasks[taskId] = {
task,
nextExecutionTime: Date.now(),
delay: Math.ceil(delay / samplingInterval) * samplingInterval,
onLoop: onLoop,
taskId,
onSuccess,
onError,
};
return taskId;
};
const update_task = (
task,
{
delay = 5000,
onLoop = true,
taskId = randHashString(12),
onSuccess = () => {},
onError = () => {},
}
) => {
const _type = typeof task;
if (_type !== "function")
throw `invalid task of type ${_type}, expected a function or a Promise`;
tasks[taskId] = {
task,
nextExecutionTime: Date.now(),
delay: Math.ceil(delay / samplingInterval) * samplingInterval,
onLoop: onLoop,
taskId,
onSuccess,
onError,
};
return taskId;
};
const remove_task = (taskId) => deleteTask(taskId);
const is_task_running = (taskId) => (tasks[taskId] ? true : false);
const remove_all_tasks = () => (tasks = {});
const get_task = (taskId) => tasks[taskId];
const get_all_tasks = () => tasks;
const eventListener = (callBack) => {
let subscription = DeviceEventEmitter.addListener(
"notificationClickHandle",
callBack
);
return function cleanup() {
subscription.remove();
};
};
const ReactNativeForegroundService = {
register,
start,
update,
stop,
stopAll,
is_running,
add_task,
update_task,
remove_task,
is_task_running,
remove_all_tasks,
get_task,
get_all_tasks,
eventListener,
};
export default ReactNativeForegroundService;