forked from ko-devHong/react-native-foreground-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (81 loc) · 2.96 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationType = void 0;
const react_native_1 = require("react-native");
const ForegroundServiceModule = react_native_1.NativeModules.LTForegroundService;
var NotificationType;
(function (NotificationType) {
NotificationType["BACKGROUND"] = "BACKGROUND";
NotificationType["FOREGROUND"] = "FOREGROUND";
})(NotificationType = exports.NotificationType || (exports.NotificationType = {}));
let stopTask = (_) => { };
let isRunning = false;
let isBackgroundRunning = false;
const generateTask = (task, parameters) => {
return async () => {
await new Promise((resolve) => {
stopTask = resolve;
task(parameters).then(() => stopTask({}));
});
};
};
const LTForegroundService = {
createNotificationChannel: async (channelConfig) => {
return await ForegroundServiceModule.createNotificationChannel(channelConfig);
},
startService: async (notificationConfig) => {
await ForegroundServiceModule.startService(notificationConfig);
isRunning = true;
},
stopService: async () => {
await ForegroundServiceModule.stopService();
isRunning = false;
},
updateService: async (notificationConfig) => {
await ForegroundServiceModule.updateService(notificationConfig);
},
backgroundStartService: async (task, backgroundConfig) => {
try {
const finalTask = generateTask(task, backgroundConfig);
const taskName = backgroundConfig.taskName ?? "BackgroundTask";
react_native_1.AppRegistry.registerHeadlessTask(taskName, () => finalTask);
if (isRunning) {
await ForegroundServiceModule.stopService();
}
await ForegroundServiceModule.backgroundStartService(backgroundConfig);
isBackgroundRunning = true;
}
catch (err) {
console.error("backgroundStartService error");
console.error(err);
}
},
backgroundStopService: async () => {
await stopTask({});
await ForegroundServiceModule.backgroundStopService();
isBackgroundRunning = false;
return;
},
startRemoteService: async (notificationConfig) => {
await ForegroundServiceModule.startRemoteService(notificationConfig);
isRunning = true;
},
stopRemoteService: async () => {
await ForegroundServiceModule.stopRemoteService();
isRunning = false;
},
updateRemoteService: async (notificationConfig) => {
await ForegroundServiceModule.updateRemoteService(notificationConfig);
},
blockNotificationChannel: async (channelConfig) => {
await ForegroundServiceModule.blockNotificationChannel(channelConfig);
},
getIsBackgroundRunning: () => {
return isBackgroundRunning;
},
getIsRunning: () => {
return isRunning;
},
};
exports.default = LTForegroundService;
//# sourceMappingURL=index.js.map