@lyxa.ai/core 1.4.163 → 1.4.165

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/index.js +11 -13
  2. package/dist/index.js.map +1 -1
  3. package/dist/libraries/index.d.ts +4 -6
  4. package/dist/libraries/index.js +6 -14
  5. package/dist/libraries/index.js.map +1 -1
  6. package/dist/libraries/notification/firebase-fcm.service.d.ts +7 -0
  7. package/dist/libraries/notification/firebase-fcm.service.js +40 -0
  8. package/dist/libraries/notification/firebase-fcm.service.js.map +1 -0
  9. package/dist/libraries/notification/firebase-message.builder.d.ts +22 -0
  10. package/dist/libraries/notification/firebase-message.builder.js +107 -0
  11. package/dist/libraries/notification/firebase-message.builder.js.map +1 -0
  12. package/dist/libraries/notification/index.d.ts +1 -1
  13. package/dist/libraries/notification/index.js.map +1 -1
  14. package/dist/libraries/notification/lyxa-fcm-sender.d.ts +13 -0
  15. package/dist/libraries/notification/lyxa-fcm-sender.js +104 -0
  16. package/dist/libraries/notification/lyxa-fcm-sender.js.map +1 -0
  17. package/dist/libraries/notification/lyxa-fcm.service.d.ts +42 -0
  18. package/dist/libraries/notification/lyxa-fcm.service.js +174 -0
  19. package/dist/libraries/notification/lyxa-fcm.service.js.map +1 -0
  20. package/dist/types/README.md +1 -1
  21. package/dist/types/package.json +1 -1
  22. package/dist/types/utilities/enum.d.ts +2 -1
  23. package/dist/types/utilities/enum.js +1 -0
  24. package/dist/types/utilities/enum.js.map +1 -1
  25. package/dist/utilities/enum.d.ts +2 -1
  26. package/dist/utilities/enum.js +1 -0
  27. package/dist/utilities/enum.js.map +1 -1
  28. package/package.json +1 -1
  29. package/dist/libraries/firebase/fcmService.d.ts +0 -38
  30. package/dist/libraries/firebase/fcmService.js +0 -267
  31. package/dist/libraries/firebase/fcmService.js.map +0 -1
@@ -1,267 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var FCMService_1;
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.FCMService = exports.FCMServiceForCpanel = exports.FCMServiceForShop = exports.FCMServiceForRider = exports.FCMServiceForUser = void 0;
14
- const firebase_admin_1 = require("firebase-admin");
15
- const __1 = require("../..");
16
- const typedi_1 = require("typedi");
17
- const enum_1 = require("../../utilities/enum");
18
- const NotificationVersionRules = {
19
- [enum_1.NotificationAccountType.RIDER]: {
20
- [enum_1.AppPlatform.IOS]: '2.0.3',
21
- [enum_1.AppPlatform.ANDROID]: '2.0.4',
22
- },
23
- [enum_1.NotificationAccountType.USER]: {
24
- [enum_1.AppPlatform.IOS]: '2.0.3',
25
- [enum_1.AppPlatform.ANDROID]: '2.0.3',
26
- },
27
- [enum_1.NotificationAccountType.SHOP]: {
28
- [enum_1.AppPlatform.IOS]: '2.0.3',
29
- [enum_1.AppPlatform.ANDROID]: '2.0.3',
30
- },
31
- [enum_1.NotificationAccountType.PROFESSIONAL]: {
32
- [enum_1.AppPlatform.IOS]: '2.0.3',
33
- [enum_1.AppPlatform.ANDROID]: '2.0.3',
34
- },
35
- };
36
- exports.FCMServiceForUser = 'fcm.service.user';
37
- exports.FCMServiceForRider = 'fcm.service.rider';
38
- exports.FCMServiceForShop = 'fcm.service.shop';
39
- exports.FCMServiceForCpanel = 'fcm.service.cpanel';
40
- let FCMService = class FCMService {
41
- static { FCMService_1 = this; }
42
- messaging;
43
- static MAX_BATCH = 500;
44
- constructor(app) {
45
- const firebaseApp = app ?? (0, __1.getLibraries)().getFirebaseService().superApp;
46
- this.messaging = (0, firebase_admin_1.messaging)(firebaseApp);
47
- }
48
- chunkArray(arr, size) {
49
- const chunks = [];
50
- for (let i = 0; i < arr.length; i += size) {
51
- chunks.push(arr.slice(i, i + size));
52
- }
53
- return chunks;
54
- }
55
- compareVersions(v1, v2) {
56
- const a = v1.split('.').map(Number);
57
- const b = v2.split('.').map(Number);
58
- for (let i = 0; i < Math.max(a.length, b.length); i++) {
59
- const diff = (a[i] || 0) - (b[i] || 0);
60
- if (diff !== 0)
61
- return diff;
62
- }
63
- return 0;
64
- }
65
- isNotifiableEntityType(type) {
66
- return (type === enum_1.NotificationAccountType.USER ||
67
- type === enum_1.NotificationAccountType.RIDER ||
68
- type === enum_1.NotificationAccountType.SHOP ||
69
- type === enum_1.NotificationAccountType.PROFESSIONAL);
70
- }
71
- isConsoleEntityType(type) {
72
- return (type === enum_1.NotificationAccountType.SHOP_CONSOLE ||
73
- type === enum_1.NotificationAccountType.PARENT ||
74
- type === enum_1.NotificationAccountType.VENDOR ||
75
- type === enum_1.NotificationAccountType.VENDOR_PARENT);
76
- }
77
- shouldUseNewStructure(entityType, platform, version) {
78
- if (!this.isNotifiableEntityType(entityType))
79
- return false;
80
- if (!platform || !version)
81
- return false;
82
- const minVersion = NotificationVersionRules?.[entityType]?.[platform];
83
- if (!minVersion)
84
- return false;
85
- return this.compareVersions(version, minVersion) >= 0;
86
- }
87
- buildConsoleMessage(payload, batchTokens) {
88
- const isInvisible = payload.invisible === true;
89
- const channelId = payload.sound ? 'order_alert' : 'default';
90
- const message = {
91
- data: {
92
- ...payload.data,
93
- channelId: isInvisible ? 'silent' : channelId,
94
- notificationTitle: payload.title ?? '',
95
- notificationBody: payload.body ?? '',
96
- },
97
- tokens: batchTokens,
98
- };
99
- return message;
100
- }
101
- buildMessage(payload, batchTokens) {
102
- const isInvisible = payload.invisible === true;
103
- const channelId = payload.sound ? 'order_alert' : 'default';
104
- const message = {
105
- data: { ...payload.data, channelId: isInvisible ? 'silent' : channelId },
106
- notification: {
107
- title: payload.title,
108
- body: payload.body,
109
- },
110
- tokens: batchTokens,
111
- android: {
112
- priority: 'high',
113
- notification: {
114
- channelId: 'silent',
115
- },
116
- },
117
- apns: {
118
- payload: {
119
- aps: {
120
- sound: payload.sound || 'default',
121
- contentAvailable: true,
122
- },
123
- },
124
- headers: {
125
- 'apns-push-type': 'background',
126
- },
127
- },
128
- };
129
- return message;
130
- }
131
- buildOldMessage(payload, batchTokens) {
132
- const isInvisible = payload.invisible === true;
133
- const message = {
134
- data: payload.data || {},
135
- notification: {
136
- title: payload.title,
137
- body: payload.body,
138
- },
139
- tokens: batchTokens,
140
- android: { priority: 'high' },
141
- apns: {
142
- payload: {
143
- aps: {
144
- sound: payload.sound || 'default',
145
- contentAvailable: true,
146
- },
147
- },
148
- },
149
- };
150
- if (!isInvisible) {
151
- message.notification = {
152
- title: payload.title,
153
- body: payload.body,
154
- };
155
- message.android = {
156
- priority: 'high',
157
- notification: {
158
- channelId: payload.sound ? 'android_Push_Notification_Channel_1' : 'default',
159
- },
160
- };
161
- }
162
- return message;
163
- }
164
- async sendNotification(payload, targets) {
165
- if (!targets.length) {
166
- return { successTokens: [], failedTokens: [] };
167
- }
168
- const logger = (0, __1.getLibraries)().getLoggerKit();
169
- const oldTokens = [];
170
- const newTokens = [];
171
- const consoleTokens = [];
172
- for (const t of targets) {
173
- if (this.isConsoleEntityType(t.entityType)) {
174
- consoleTokens.push(t.token);
175
- continue;
176
- }
177
- const useNew = this.shouldUseNewStructure(t.entityType, t.platform, t.appVersion);
178
- if (useNew)
179
- newTokens.push(t.token);
180
- else
181
- oldTokens.push(t.token);
182
- }
183
- const sendGroup = async (data) => {
184
- const tokenChunks = this.chunkArray(data.groupTokens, FCMService_1.MAX_BATCH);
185
- const success = [];
186
- const failed = [];
187
- const results = await Promise.all(tokenChunks.map(async (chunk) => {
188
- const message = data.consoleNotification
189
- ? this.buildConsoleMessage(data.groupPayload, chunk)
190
- : data.oldNotificationStructure
191
- ? this.buildOldMessage(data.groupPayload, chunk)
192
- : this.buildMessage(data.groupPayload, chunk);
193
- const response = await this.messaging.sendEachForMulticast(message);
194
- const logEntry = {
195
- timestamp: new Date().toISOString(),
196
- groupTokens: data.groupTokens,
197
- groupPayload: data.groupPayload,
198
- oldNotificationStructure: data.oldNotificationStructure,
199
- consoleNotification: data.consoleNotification,
200
- message,
201
- response,
202
- };
203
- logger.debug('Called Firebase function sendEachForMulticast', {
204
- tag: enum_1.LogTag.NOTIFICATION_ATTEMPTED,
205
- data: logEntry,
206
- });
207
- const batchSuccess = [];
208
- const batchFailed = [];
209
- response.responses.forEach((resp, idx) => {
210
- if (resp.success) {
211
- batchSuccess.push(chunk[idx]);
212
- }
213
- else {
214
- const errorCode = resp.error?.code;
215
- if (errorCode === 'messaging/invalid-argument' ||
216
- errorCode === 'messaging/registration-token-not-registered') {
217
- batchFailed.push(chunk[idx]);
218
- }
219
- console.warn(`FCM Error for token ${chunk[idx]}:`, resp.error?.message);
220
- }
221
- });
222
- return { batchSuccess, batchFailed };
223
- }));
224
- for (const r of results) {
225
- success.push(...r.batchSuccess);
226
- failed.push(...r.batchFailed);
227
- }
228
- return { success, failed };
229
- };
230
- let successTokens = [];
231
- let failedTokens = [];
232
- if (oldTokens.length) {
233
- const r = await sendGroup({
234
- groupTokens: oldTokens,
235
- groupPayload: payload,
236
- oldNotificationStructure: true,
237
- });
238
- successTokens.push(...r.success);
239
- failedTokens.push(...r.failed);
240
- }
241
- if (newTokens.length) {
242
- const r = await sendGroup({
243
- groupTokens: newTokens,
244
- groupPayload: payload,
245
- oldNotificationStructure: false,
246
- });
247
- successTokens.push(...r.success);
248
- failedTokens.push(...r.failed);
249
- }
250
- if (consoleTokens.length) {
251
- const r = await sendGroup({
252
- groupTokens: consoleTokens,
253
- groupPayload: payload,
254
- consoleNotification: true,
255
- });
256
- successTokens.push(...r.success);
257
- failedTokens.push(...r.failed);
258
- }
259
- return { successTokens, failedTokens };
260
- }
261
- };
262
- exports.FCMService = FCMService;
263
- exports.FCMService = FCMService = FCMService_1 = __decorate([
264
- (0, typedi_1.Service)(),
265
- __metadata("design:paramtypes", [Object])
266
- ], FCMService);
267
- //# sourceMappingURL=fcmService.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fcmService.js","sourceRoot":"/","sources":["libraries/firebase/fcmService.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,mDAA2C;AAC3C,6BAAqC;AACrC,mCAAiC;AAEjC,+CAAoF;AAyBpF,MAAM,wBAAwB,GAAuE;IACpG,CAAC,8BAAuB,CAAC,KAAK,CAAC,EAAE;QAChC,CAAC,kBAAW,CAAC,GAAG,CAAC,EAAE,OAAO;QAC1B,CAAC,kBAAW,CAAC,OAAO,CAAC,EAAE,OAAO;KAC9B;IAED,CAAC,8BAAuB,CAAC,IAAI,CAAC,EAAE;QAC/B,CAAC,kBAAW,CAAC,GAAG,CAAC,EAAE,OAAO;QAC1B,CAAC,kBAAW,CAAC,OAAO,CAAC,EAAE,OAAO;KAC9B;IAED,CAAC,8BAAuB,CAAC,IAAI,CAAC,EAAE;QAC/B,CAAC,kBAAW,CAAC,GAAG,CAAC,EAAE,OAAO;QAC1B,CAAC,kBAAW,CAAC,OAAO,CAAC,EAAE,OAAO;KAC9B;IAED,CAAC,8BAAuB,CAAC,YAAY,CAAC,EAAE;QACvC,CAAC,kBAAW,CAAC,GAAG,CAAC,EAAE,OAAO;QAC1B,CAAC,kBAAW,CAAC,OAAO,CAAC,EAAE,OAAO;KAC9B;CACD,CAAC;AAEW,QAAA,iBAAiB,GAAG,kBAAkB,CAAC;AACvC,QAAA,kBAAkB,GAAG,mBAAmB,CAAC;AACzC,QAAA,iBAAiB,GAAG,kBAAkB,CAAC;AACvC,QAAA,mBAAmB,GAAG,oBAAoB,CAAC;AAGjD,IAAM,UAAU,GAAhB,MAAM,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a,EAAE,CAAC;QACjC,IAAI,YAAY,GAAa,EAAE,CAAC;QAGhC,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;YACtB,MAAM,CAAC,GAAG,MAAM,SAAS,CAAC;gBACzB,WAAW,EAAE,SAAS;gBACtB,YAAY,EAAE,OAAO;gBACrB,wBAAwB,EAAE,IAAI;aAC9B,CAAC,CAAC;YAEH,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC;YACjC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;QAChC,CAAC;QAGD,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;YACtB,MAAM,CAAC,GAAG,MAAM,SAAS,CAAC;gBACzB,WAAW,EAAE,SAAS;gBACtB,YAAY,EAAE,OAAO;gBACrB,wBAAwB,EAAE,KAAK;aAC/B,CAAC,CAAC;YAEH,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC;YACjC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;QAChC,CAAC;QAGD,IAAI,aAAa,CAAC,MAAM,EAAE,CAAC;YAC1B,MAAM,CAAC,GAAG,MAAM,SAAS,CAAC;gBACzB,WAAW,EAAE,aAAa;gBAC1B,YAAY,EAAE,OAAO;gBACrB,mBAAmB,EAAE,IAAI;aACzB,CAAC,CAAC;YAEH,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC;YACjC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;QAChC,CAAC;QAED,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,CAAC;IACxC,CAAC;;AArTW,gCAAU;qBAAV,UAAU;IADtB,IAAA,gBAAO,GAAE;;GACG,UAAU,CAsTtB","sourcesContent":["import { messaging } from 'firebase-admin';\nimport { getLibraries } from '../..';\nimport { Service } from 'typedi';\nimport { App } from 'firebase-admin/app';\nimport { AppPlatform, LogTag, NotificationAccountType } from '../../utilities/enum';\n\nexport interface FCMNotificationPayload {\n\ttitle?: string;\n\tbody?: string;\n\tdata?: Record<string, string>;\n\tsound?: string;\n\tinvisible?: boolean;\n}\n\nexport interface NotificationTokenTarget {\n\ttoken: string;\n\tentityType: NotificationAccountType;\n\tdeviceId?: string;\n\tentityId?: string;\n\tplatform?: AppPlatform;\n\tappVersion?: string;\n}\n\ntype NotifiableEntityType =\n\t| NotificationAccountType.USER\n\t| NotificationAccountType.SHOP\n\t| NotificationAccountType.RIDER\n\t| NotificationAccountType.PROFESSIONAL;\n\nconst NotificationVersionRules: Record<NotifiableEntityType, Partial<Record<AppPlatform, string>>> = {\n\t[NotificationAccountType.RIDER]: {\n\t\t[AppPlatform.IOS]: '2.0.3',\n\t\t[AppPlatform.ANDROID]: '2.0.4',\n\t},\n\n\t[NotificationAccountType.USER]: {\n\t\t[AppPlatform.IOS]: '2.0.3',\n\t\t[AppPlatform.ANDROID]: '2.0.3',\n\t},\n\n\t[NotificationAccountType.SHOP]: {\n\t\t[AppPlatform.IOS]: '2.0.3',\n\t\t[AppPlatform.ANDROID]: '2.0.3',\n\t},\n\n\t[NotificationAccountType.PROFESSIONAL]: {\n\t\t[AppPlatform.IOS]: '2.0.3',\n\t\t[AppPlatform.ANDROID]: '2.0.3',\n\t},\n};\n\nexport const FCMServiceForUser = 'fcm.service.user';\nexport const FCMServiceForRider = 'fcm.service.rider';\nexport const FCMServiceForShop = 'fcm.service.shop';\nexport const FCMServiceForCpanel = 'fcm.service.cpanel';\n\n@Service()\nexport class FCMService {\n\tprivate messaging!: messaging.Messaging;\n\tprivate static readonly MAX_BATCH = 500;\n\n\tconstructor(app?: App) {\n\t\tconst firebaseApp = app ?? getLibraries().getFirebaseService().superApp;\n\t\tthis.messaging = messaging(firebaseApp);\n\t}\n\n\t/**\n\t * Split array into chunks of fixed size\n\t */\n\tprivate chunkArray<T>(arr: T[], size: number): T[][] {\n\t\tconst chunks: T[][] = [];\n\t\tfor (let i = 0; i < arr.length; i += size) {\n\t\t\tchunks.push(arr.slice(i, i + size));\n\t\t}\n\t\treturn chunks;\n\t}\n\n\tprivate compareVersions(v1: string, v2: string): number {\n\t\tconst a = v1.split('.').map(Number);\n\t\tconst b = v2.split('.').map(Number);\n\n\t\tfor (let i = 0; i < Math.max(a.length, b.length); i++) {\n\t\t\tconst diff = (a[i] || 0) - (b[i] || 0);\n\t\t\tif (diff !== 0) return diff;\n\t\t}\n\n\t\treturn 0;\n\t}\n\n\tprivate isNotifiableEntityType(type: NotificationAccountType): type is NotifiableEntityType {\n\t\treturn (\n\t\t\ttype === NotificationAccountType.USER ||\n\t\t\ttype === NotificationAccountType.RIDER ||\n\t\t\ttype === NotificationAccountType.SHOP ||\n\t\t\ttype === NotificationAccountType.PROFESSIONAL\n\t\t);\n\t}\n\n\tprivate isConsoleEntityType(type: NotificationAccountType): type is NotifiableEntityType {\n\t\treturn (\n\t\t\ttype === NotificationAccountType.SHOP_CONSOLE ||\n\t\t\ttype === NotificationAccountType.PARENT ||\n\t\t\ttype === NotificationAccountType.VENDOR ||\n\t\t\ttype === NotificationAccountType.VENDOR_PARENT\n\t\t);\n\t}\n\n\tprivate shouldUseNewStructure(\n\t\tentityType: NotificationAccountType,\n\t\tplatform?: AppPlatform,\n\t\tversion?: string\n\t): boolean {\n\t\tif (!this.isNotifiableEntityType(entityType)) return false;\n\n\t\tif (!platform || !version) return false;\n\n\t\tconst minVersion = NotificationVersionRules?.[entityType]?.[platform];\n\n\t\tif (!minVersion) return false;\n\n\t\treturn this.compareVersions(version, minVersion) >= 0;\n\t}\n\n\t/**\n\t * Build multicast message safely\n\t */\n\tprivate buildConsoleMessage(\n\t\tpayload: FCMNotificationPayload,\n\t\tbatchTokens: string[]\n\t): messaging.MulticastMessage {\n\t\tconst isInvisible = payload.invisible === true;\n\t\tconst channelId = payload.sound ? 'order_alert' : 'default';\n\n\t\tconst message: messaging.MulticastMessage = {\n\t\t\tdata: {\n\t\t\t\t...payload.data,\n\t\t\t\tchannelId: isInvisible ? 'silent' : channelId,\n\t\t\t\tnotificationTitle: payload.title ?? '',\n\t\t\t\tnotificationBody: payload.body ?? '',\n\t\t\t},\n\t\t\ttokens: batchTokens,\n\t\t};\n\n\t\treturn message;\n\t}\n\n\t/**\n\t * Build multicast message safely\n\t */\n\tprivate buildMessage(payload: FCMNotificationPayload, batchTokens: string[]): messaging.MulticastMessage {\n\t\tconst isInvisible = payload.invisible === true;\n\t\tconst channelId = payload.sound ? 'order_alert' : 'default';\n\n\t\tconst message: messaging.MulticastMessage = {\n\t\t\tdata: { ...payload.data, channelId: isInvisible ? 'silent' : channelId },\n\t\t\tnotification: {\n\t\t\t\ttitle: payload.title,\n\t\t\t\tbody: payload.body,\n\t\t\t},\n\t\t\ttokens: batchTokens,\n\t\t\tandroid: {\n\t\t\t\tpriority: 'high',\n\t\t\t\tnotification: {\n\t\t\t\t\tchannelId: 'silent',\n\t\t\t\t},\n\t\t\t},\n\t\t\tapns: {\n\t\t\t\tpayload: {\n\t\t\t\t\taps: {\n\t\t\t\t\t\tsound: payload.sound || 'default',\n\t\t\t\t\t\tcontentAvailable: true,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\theaders: {\n\t\t\t\t\t'apns-push-type': 'background',\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\n\t\treturn message;\n\t}\n\n\t/**\n\t * Build multicast message safely for old apps\n\t */\n\tprivate buildOldMessage(\n\t\tpayload: FCMNotificationPayload,\n\t\tbatchTokens: string[]\n\t): messaging.MulticastMessage {\n\t\tconst isInvisible = payload.invisible === true;\n\n\t\tconst message: messaging.MulticastMessage = {\n\t\t\tdata: payload.data || {},\n\t\t\tnotification: {\n\t\t\t\ttitle: payload.title,\n\t\t\t\tbody: payload.body,\n\t\t\t},\n\t\t\ttokens: batchTokens,\n\t\t\tandroid: { priority: 'high' },\n\t\t\tapns: {\n\t\t\t\tpayload: {\n\t\t\t\t\taps: {\n\t\t\t\t\t\tsound: payload.sound || 'default',\n\t\t\t\t\t\tcontentAvailable: true,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\n\t\tif (!isInvisible) {\n\t\t\tmessage.notification = {\n\t\t\t\ttitle: payload.title,\n\t\t\t\tbody: payload.body,\n\t\t\t};\n\n\t\t\tmessage.android = {\n\t\t\t\tpriority: 'high',\n\t\t\t\tnotification: {\n\t\t\t\t\tchannelId: payload.sound ? 'android_Push_Notification_Channel_1' : 'default',\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\treturn message;\n\t}\n\n\t/**\n\t * Sends a push notification (visible or invisible) to multiple device tokens\n\t * @param payload - Notification title, body, and optional data\n\t * @param tokens - FCM device tokens\n\t * @returns Object containing success and invalid tokens\n\t */\n\tasync sendNotification(\n\t\tpayload: FCMNotificationPayload,\n\t\ttargets: NotificationTokenTarget[]\n\t): Promise<{ successTokens: string[]; failedTokens: string[] }> {\n\t\tif (!targets.length) {\n\t\t\treturn { successTokens: [], failedTokens: [] };\n\t\t}\n\n\t\tconst logger = getLibraries().getLoggerKit();\n\n\t\tconst oldTokens: string[] = [];\n\t\tconst newTokens: string[] = [];\n\t\tconst consoleTokens: string[] = [];\n\n\t\tfor (const t of targets) {\n\t\t\tif (this.isConsoleEntityType(t.entityType)) {\n\t\t\t\tconsoleTokens.push(t.token);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst useNew = this.shouldUseNewStructure(t.entityType, t.platform, t.appVersion);\n\n\t\t\tif (useNew) newTokens.push(t.token);\n\t\t\telse oldTokens.push(t.token);\n\t\t}\n\n\t\tconst sendGroup = async (data: {\n\t\t\tgroupTokens: string[];\n\t\t\tgroupPayload: FCMNotificationPayload;\n\t\t\toldNotificationStructure?: boolean;\n\t\t\tconsoleNotification?: boolean;\n\t\t}) => {\n\t\t\tconst tokenChunks = this.chunkArray(data.groupTokens, FCMService.MAX_BATCH);\n\n\t\t\tconst success: string[] = [];\n\t\t\tconst failed: string[] = [];\n\n\t\t\tconst results = await Promise.all(\n\t\t\t\ttokenChunks.map(async chunk => {\n\t\t\t\t\tconst message = data.consoleNotification\n\t\t\t\t\t\t? this.buildConsoleMessage(data.groupPayload, chunk)\n\t\t\t\t\t\t: data.oldNotificationStructure\n\t\t\t\t\t\t\t? this.buildOldMessage(data.groupPayload, chunk)\n\t\t\t\t\t\t\t: this.buildMessage(data.groupPayload, chunk);\n\n\t\t\t\t\tconst response = await this.messaging.sendEachForMulticast(message);\n\n\t\t\t\t\tconst logEntry = {\n\t\t\t\t\t\ttimestamp: new Date().toISOString(),\n\t\t\t\t\t\tgroupTokens: data.groupTokens,\n\t\t\t\t\t\tgroupPayload: data.groupPayload,\n\t\t\t\t\t\toldNotificationStructure: data.oldNotificationStructure,\n\t\t\t\t\t\tconsoleNotification: data.consoleNotification,\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t};\n\n\t\t\t\t\tlogger.debug('Called Firebase function sendEachForMulticast', {\n\t\t\t\t\t\ttag: LogTag.NOTIFICATION_ATTEMPTED,\n\t\t\t\t\t\tdata: logEntry,\n\t\t\t\t\t});\n\n\t\t\t\t\tconst batchSuccess: string[] = [];\n\t\t\t\t\tconst batchFailed: string[] = [];\n\n\t\t\t\t\tresponse.responses.forEach((resp, idx) => {\n\t\t\t\t\t\tif (resp.success) {\n\t\t\t\t\t\t\tbatchSuccess.push(chunk[idx]);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tconst errorCode = resp.error?.code;\n\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\terrorCode === 'messaging/invalid-argument' ||\n\t\t\t\t\t\t\t\terrorCode === 'messaging/registration-token-not-registered'\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tbatchFailed.push(chunk[idx]);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconsole.warn(`FCM Error for token ${chunk[idx]}:`, resp.error?.message);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\treturn { batchSuccess, batchFailed };\n\t\t\t\t})\n\t\t\t);\n\n\t\t\tfor (const r of results) {\n\t\t\t\tsuccess.push(...r.batchSuccess);\n\t\t\t\tfailed.push(...r.batchFailed);\n\t\t\t}\n\n\t\t\treturn { success, failed };\n\t\t};\n\n\t\tlet successTokens: string[] = [];\n\t\tlet failedTokens: string[] = [];\n\n\t\t// send old payload\n\t\tif (oldTokens.length) {\n\t\t\tconst r = await sendGroup({\n\t\t\t\tgroupTokens: oldTokens,\n\t\t\t\tgroupPayload: payload,\n\t\t\t\toldNotificationStructure: true,\n\t\t\t});\n\n\t\t\tsuccessTokens.push(...r.success);\n\t\t\tfailedTokens.push(...r.failed);\n\t\t}\n\n\t\t// send new payload\n\t\tif (newTokens.length) {\n\t\t\tconst r = await sendGroup({\n\t\t\t\tgroupTokens: newTokens,\n\t\t\t\tgroupPayload: payload,\n\t\t\t\toldNotificationStructure: false,\n\t\t\t});\n\n\t\t\tsuccessTokens.push(...r.success);\n\t\t\tfailedTokens.push(...r.failed);\n\t\t}\n\n\t\t// send console tokens\n\t\tif (consoleTokens.length) {\n\t\t\tconst r = await sendGroup({\n\t\t\t\tgroupTokens: consoleTokens,\n\t\t\t\tgroupPayload: payload,\n\t\t\t\tconsoleNotification: true,\n\t\t\t});\n\n\t\t\tsuccessTokens.push(...r.success);\n\t\t\tfailedTokens.push(...r.failed);\n\t\t}\n\n\t\treturn { successTokens, failedTokens };\n\t}\n}\n"]}