@quatrain/messaging-firebase 1.0.1
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.
- package/lib/FirebaseMessagingAdapter.d.ts +21 -0
- package/lib/FirebaseMessagingAdapter.js +110 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/package.json +41 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AbstractMessagingAdapter, MessagingParameters, MessagingRecipient, NotificationCapableAdapter, NotificationMessage } from '@quatrain/messaging';
|
|
2
|
+
export declare class FirebaseMessagingAdapter extends AbstractMessagingAdapter implements NotificationCapableAdapter {
|
|
3
|
+
private _messaging;
|
|
4
|
+
constructor(params?: MessagingParameters);
|
|
5
|
+
private _initializeFirebase;
|
|
6
|
+
/**
|
|
7
|
+
* Sends a notification to a single recipient via Firebase Cloud Messaging
|
|
8
|
+
*
|
|
9
|
+
* @param recipient - The message recipient containing contact info and FCM token
|
|
10
|
+
* @param message - The notification message with title, body, and optional data
|
|
11
|
+
* @returns Promise<string> - The Firebase message ID on successful delivery
|
|
12
|
+
* @throws Error if recipient has no message token or Firebase send fails
|
|
13
|
+
*/
|
|
14
|
+
sendNotification(recipient: MessagingRecipient, message: NotificationMessage): Promise<string>;
|
|
15
|
+
sendNotifications(recipients: MessagingRecipient[], message: NotificationMessage): Promise<{
|
|
16
|
+
successCount: number;
|
|
17
|
+
failureCount: number;
|
|
18
|
+
responses: any[];
|
|
19
|
+
}>;
|
|
20
|
+
private _serializeData;
|
|
21
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FirebaseMessagingAdapter = void 0;
|
|
13
|
+
const messaging_1 = require("@quatrain/messaging");
|
|
14
|
+
const app_1 = require("firebase-admin/app");
|
|
15
|
+
const messaging_2 = require("firebase-admin/messaging");
|
|
16
|
+
class FirebaseMessagingAdapter extends messaging_1.AbstractMessagingAdapter {
|
|
17
|
+
constructor(params = {}) {
|
|
18
|
+
super(params);
|
|
19
|
+
this._initializeFirebase();
|
|
20
|
+
}
|
|
21
|
+
_initializeFirebase() {
|
|
22
|
+
try {
|
|
23
|
+
if ((0, app_1.getApps)().length === 0) {
|
|
24
|
+
if (!this._params.config) {
|
|
25
|
+
throw new Error('Firebase config is required');
|
|
26
|
+
}
|
|
27
|
+
(0, app_1.initializeApp)(this._params.config);
|
|
28
|
+
}
|
|
29
|
+
this._messaging = (0, messaging_2.getMessaging)();
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
messaging_1.Messaging.error(`Failed to initialize Firebase: ${error.message}`);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Sends a notification to a single recipient via Firebase Cloud Messaging
|
|
38
|
+
*
|
|
39
|
+
* @param recipient - The message recipient containing contact info and FCM token
|
|
40
|
+
* @param message - The notification message with title, body, and optional data
|
|
41
|
+
* @returns Promise<string> - The Firebase message ID on successful delivery
|
|
42
|
+
* @throws Error if recipient has no message token or Firebase send fails
|
|
43
|
+
*/
|
|
44
|
+
sendNotification(recipient, message) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
try {
|
|
47
|
+
if (!recipient.messageToken) {
|
|
48
|
+
throw new Error('Recipient message token is required');
|
|
49
|
+
}
|
|
50
|
+
const firebaseMessage = {
|
|
51
|
+
token: message.token || recipient.messageToken,
|
|
52
|
+
notification: {
|
|
53
|
+
title: message.title,
|
|
54
|
+
body: message.body || '',
|
|
55
|
+
},
|
|
56
|
+
data: this._serializeData(message.data || {}),
|
|
57
|
+
};
|
|
58
|
+
const response = yield this._messaging.send(firebaseMessage);
|
|
59
|
+
messaging_1.Messaging.info(`Notification sent successfully to ${recipient.firstname} ${recipient.lastname}: ${response}`);
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
const errorMessage = `Failed to send notification to ${recipient.firstname} ${recipient.lastname}: ${error.message}`;
|
|
64
|
+
messaging_1.Messaging.error(errorMessage);
|
|
65
|
+
throw new Error(errorMessage);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
sendNotifications(recipients, message) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
try {
|
|
72
|
+
const tokens = recipients
|
|
73
|
+
.map((recipient) => recipient.messageToken)
|
|
74
|
+
.filter((token) => token);
|
|
75
|
+
if (tokens.length === 0) {
|
|
76
|
+
throw new Error('No valid message tokens found');
|
|
77
|
+
}
|
|
78
|
+
const multicastMessage = {
|
|
79
|
+
tokens,
|
|
80
|
+
notification: {
|
|
81
|
+
title: message.title,
|
|
82
|
+
body: message.body || '',
|
|
83
|
+
},
|
|
84
|
+
data: this._serializeData(message.data || {}),
|
|
85
|
+
};
|
|
86
|
+
const response = yield this._messaging.sendMulticast(multicastMessage);
|
|
87
|
+
messaging_1.Messaging.info(`Multicast notification sent: ${response.successCount} successful, ${response.failureCount} failed`);
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const errorMessage = `Failed to send multicast notification: ${error.message}`;
|
|
92
|
+
messaging_1.Messaging.error(errorMessage);
|
|
93
|
+
throw new Error(errorMessage);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
_serializeData(data) {
|
|
98
|
+
const serialized = {};
|
|
99
|
+
for (const [key, value] of Object.entries(data)) {
|
|
100
|
+
if (typeof value === 'string') {
|
|
101
|
+
serialized[key] = value;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
serialized[key] = JSON.stringify(value);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return serialized;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.FirebaseMessagingAdapter = FirebaseMessagingAdapter;
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FirebaseMessagingAdapter = void 0;
|
|
4
|
+
const FirebaseMessagingAdapter_1 = require("./FirebaseMessagingAdapter");
|
|
5
|
+
Object.defineProperty(exports, "FirebaseMessagingAdapter", { enumerable: true, get: function () { return FirebaseMessagingAdapter_1.FirebaseMessagingAdapter; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quatrain/messaging-firebase",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"author": "Quatrain Développement SAS <developers@quatrain.com>",
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@quatrain/core": "^1.1.19"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@quatrain/messaging": "^1.0.0",
|
|
17
|
+
"firebase-admin": "^13.0.1"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@tsconfig/recommended": "^1.0.1",
|
|
21
|
+
"@types/jest": "^27.0.3",
|
|
22
|
+
"@types/node": "^22.10.1",
|
|
23
|
+
"jest": "^27.4.7",
|
|
24
|
+
"jest-node-exports-resolver": "^1.1.6",
|
|
25
|
+
"jest-serial-runner": "^1.2.1",
|
|
26
|
+
"trace-unhandled": "^2.0.1",
|
|
27
|
+
"ts-jest": "^27.1.2",
|
|
28
|
+
"typescript": "^5.1.5"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc",
|
|
32
|
+
"wbuild": "tsc --watch",
|
|
33
|
+
"test": "jest",
|
|
34
|
+
"bump-to": "yarn version",
|
|
35
|
+
"hash": "node ../../bin/hashFolder.js",
|
|
36
|
+
"hash:persist": "yarn hash > .hash_latest.txt",
|
|
37
|
+
"hash:compare": "yarn hash > .hash_newest.txt && cmp -s .hash_latest.txt .hash_newest.txt",
|
|
38
|
+
"publish": "yarn hash:compare || yarn publish:process",
|
|
39
|
+
"publish:process": "yarn version patch && yarn build && yarn npm publish --access public && yarn hash:persist"
|
|
40
|
+
}
|
|
41
|
+
}
|