feature/IO-3492-FCM-Queue-For-Notifications: Checkpoint
This commit is contained in:
@@ -3187,3 +3187,12 @@ mutation INSERT_MEDIA_ANALYTICS($mediaObject: media_analytics_insert_input!) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports.GET_USERS_FCM_TOKENS_BY_EMAILS = /* GraphQL */ `
|
||||||
|
query GET_USERS_FCM_TOKENS_BY_EMAILS($emails: [String!]!) {
|
||||||
|
users(where: { email: { _in: $emails } }) {
|
||||||
|
email
|
||||||
|
fcmtokens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
|
// NOTE: Despite the filename, this implementation targets Expo Push Tokens (ExponentPushToken[...]).
|
||||||
|
// It does NOT use Firebase Admin and does NOT require credentials (no EXPO_ACCESS_TOKEN).
|
||||||
|
|
||||||
const { Queue, Worker } = require("bullmq");
|
const { Queue, Worker } = require("bullmq");
|
||||||
const { registerCleanupTask } = require("../../utils/cleanupManager");
|
const { registerCleanupTask } = require("../../utils/cleanupManager");
|
||||||
const getBullMQPrefix = require("../../utils/getBullMQPrefix");
|
const getBullMQPrefix = require("../../utils/getBullMQPrefix");
|
||||||
const devDebugLogger = require("../../utils/devDebugLogger");
|
const devDebugLogger = require("../../utils/devDebugLogger");
|
||||||
|
|
||||||
|
const { client: gqlClient } = require("../../graphql-client/graphql-client");
|
||||||
|
const { GET_USERS_FCM_TOKENS_BY_EMAILS } = require("../../graphql-client/queries");
|
||||||
|
|
||||||
const FCM_CONSOLIDATION_DELAY_IN_MINS = (() => {
|
const FCM_CONSOLIDATION_DELAY_IN_MINS = (() => {
|
||||||
const envValue = process.env?.FCM_CONSOLIDATION_DELAY_IN_MINS;
|
const envValue = process.env?.FCM_CONSOLIDATION_DELAY_IN_MINS;
|
||||||
const parsedValue = envValue ? parseInt(envValue, 10) : NaN;
|
const parsedValue = envValue ? parseInt(envValue, 10) : NaN;
|
||||||
@@ -17,35 +23,173 @@ const LOCK_EXPIRATION = FCM_CONSOLIDATION_DELAY * 0.25;
|
|||||||
const RATE_LIMITER_DURATION = FCM_CONSOLIDATION_DELAY * 0.1;
|
const RATE_LIMITER_DURATION = FCM_CONSOLIDATION_DELAY * 0.1;
|
||||||
const NOTIFICATION_EXPIRATION = FCM_CONSOLIDATION_DELAY * 1.5;
|
const NOTIFICATION_EXPIRATION = FCM_CONSOLIDATION_DELAY * 1.5;
|
||||||
|
|
||||||
|
const EXPO_PUSH_ENDPOINT = "https://exp.host/--/api/v2/push/send";
|
||||||
|
const EXPO_MAX_MESSAGES_PER_REQUEST = 100;
|
||||||
|
|
||||||
let fcmAddQueue;
|
let fcmAddQueue;
|
||||||
let fcmConsolidateQueue;
|
let fcmConsolidateQueue;
|
||||||
let fcmAddWorker;
|
let fcmAddWorker;
|
||||||
let fcmConsolidateWorker;
|
let fcmConsolidateWorker;
|
||||||
|
|
||||||
// IMPORTANT: do NOT require firebase-handler at module load time.
|
/**
|
||||||
// firebase-handler does `require(process.env.FIREBASE_ADMINSDK_JSON)` at top-level,
|
* Milliseconds to seconds.
|
||||||
// which will hard-crash environments that don’t have Firebase configured.
|
* @param ms
|
||||||
const hasFirebaseEnv = () => Boolean(process.env.FIREBASE_ADMINSDK_JSON && process.env.FIREBASE_DATABASE_URL);
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
const seconds = (ms) => Math.max(1, Math.ceil(ms / 1000));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Firebase Admin SDK, or null if Firebase is not configured.
|
* Chunk an array into smaller arrays of given size.
|
||||||
* @returns {{app: app, remoteConfig: ((app?: App) => remoteConfig.RemoteConfig) | remoteConfig, firestore: ((app?: App) => FirebaseFirestore.Firestore) | firestore, AppOptions: AppOptions, auth: ((app?: App) => auth.Auth) | auth, securityRules: ((app?: App) => securityRules.SecurityRules) | securityRules, installations: ((app?: App) => installations.Installations) | installations, FirebaseArrayIndexError: FirebaseArrayIndexError, storage: ((app?: App) => storage.Storage) | storage, appCheck: ((app?: App) => appCheck.AppCheck) | appCheck, initializeApp(options?: AppOptions, name?: string): app.App, FirebaseError: FirebaseError, messaging: ((app?: App) => messaging.Messaging) | messaging, projectManagement: ((app?: App) => projectManagement.ProjectManagement) | projectManagement, database: ((app?: App) => database.Database) | database, machineLearning: ((app?: App) => machineLearning.MachineLearning) | machineLearning, instanceId: ((app?: App) => instanceId.InstanceId) | instanceId, SDK_VERSION: string, apps: (app.App | null)[], credential: credential, ServiceAccount: ServiceAccount, GoogleOAuthAccessToken: GoogleOAuthAccessToken}|null}
|
* @param arr
|
||||||
|
* @param size
|
||||||
|
* @returns {*[]}
|
||||||
*/
|
*/
|
||||||
const getFirebaseAdmin = () => {
|
const chunk = (arr, size) => {
|
||||||
if (!hasFirebaseEnv()) return null;
|
const out = [];
|
||||||
const { admin } = require("../../firebase/firebase-handler");
|
for (let i = 0; i < arr.length; i += size) out.push(arr.slice(i, i + size));
|
||||||
return admin;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the FCM topic name for an association.
|
* Check if a string is an Expo push token.
|
||||||
* @param associationId
|
* @param s
|
||||||
* @returns {`assoc-${string}-notifications`}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
const topicForAssociation = (associationId) => `assoc-${associationId}-notifications`;
|
const isExpoPushToken = (s) => {
|
||||||
|
if (!s || typeof s !== "string") return false;
|
||||||
|
// Common formats observed in the wild:
|
||||||
|
// - ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]
|
||||||
|
// - ExpoPushToken[xxxxxxxxxxxxxxxxxxxxxx]
|
||||||
|
return /^ExponentPushToken\[[^\]]+\]$/.test(s) || /^ExpoPushToken\[[^\]]+\]$/.test(s);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a summary string for FCM push notification body.
|
* Get unique, trimmed strings from an array.
|
||||||
|
* @param arr
|
||||||
|
* @returns {any[]}
|
||||||
|
*/
|
||||||
|
const uniqStrings = (arr) => [
|
||||||
|
...new Set(
|
||||||
|
arr
|
||||||
|
.filter(Boolean)
|
||||||
|
.map((x) => String(x).trim())
|
||||||
|
.filter(Boolean)
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize users.fcmtokens (jsonb) into an array of Expo push tokens.
|
||||||
|
*
|
||||||
|
* New expected shape (example):
|
||||||
|
* {
|
||||||
|
* "ExponentPushToken[dksJAdLUTofdEk7P59thue]": {
|
||||||
|
* "platform": "ios",
|
||||||
|
* "timestamp": 1767397802709,
|
||||||
|
* "pushTokenString": "ExponentPushToken[dksJAdLUTofdEk7P59thue]"
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* Also supports older/alternate shapes:
|
||||||
|
* - string: "ExponentPushToken[...]"
|
||||||
|
* - array: ["ExponentPushToken[...]", ...]
|
||||||
|
* - object: token keys OR values containing token-like fields
|
||||||
|
* @param fcmtokens
|
||||||
|
* @returns {string[]|*[]}
|
||||||
|
*/
|
||||||
|
const normalizeTokens = (fcmtokens) => {
|
||||||
|
if (!fcmtokens) return [];
|
||||||
|
|
||||||
|
if (typeof fcmtokens === "string") {
|
||||||
|
const s = fcmtokens.trim();
|
||||||
|
return isExpoPushToken(s) ? [s] : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(fcmtokens)) {
|
||||||
|
return uniqStrings(fcmtokens).filter(isExpoPushToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof fcmtokens === "object") {
|
||||||
|
const keys = Object.keys(fcmtokens || {});
|
||||||
|
const vals = Object.values(fcmtokens || {});
|
||||||
|
|
||||||
|
const fromKeys = keys.filter(isExpoPushToken);
|
||||||
|
|
||||||
|
const fromValues = vals
|
||||||
|
.map((v) => {
|
||||||
|
if (!v) return null;
|
||||||
|
|
||||||
|
// Some shapes store token as a string value directly
|
||||||
|
if (typeof v === "string") return v;
|
||||||
|
|
||||||
|
if (typeof v === "object") {
|
||||||
|
// Your new shape uses pushTokenString
|
||||||
|
return v.pushTokenString || v.token || v.expoPushToken || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
.map(String);
|
||||||
|
|
||||||
|
return uniqStrings([...fromKeys, ...fromValues]).filter(isExpoPushToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely parse JSON response.
|
||||||
|
* @param res
|
||||||
|
* @returns {Promise<*|null>}
|
||||||
|
*/
|
||||||
|
const safeJson = async (res) => {
|
||||||
|
try {
|
||||||
|
return await res.json();
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send Expo push notifications
|
||||||
|
* @param {Array<Object>} messages Expo messages array
|
||||||
|
* @param {Object} logger
|
||||||
|
*/
|
||||||
|
const sendExpoPush = async ({ messages, logger }) => {
|
||||||
|
if (!messages?.length) return;
|
||||||
|
|
||||||
|
for (const batch of chunk(messages, EXPO_MAX_MESSAGES_PER_REQUEST)) {
|
||||||
|
const res = await fetch(EXPO_PUSH_ENDPOINT, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
accept: "application/json",
|
||||||
|
"content-type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify(batch)
|
||||||
|
});
|
||||||
|
|
||||||
|
const payload = await safeJson(res);
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
logger?.log?.("expo-push-http-error", "ERROR", "notifications", "api", {
|
||||||
|
status: res.status,
|
||||||
|
statusText: res.statusText,
|
||||||
|
payload
|
||||||
|
});
|
||||||
|
throw new Error(`Expo push HTTP error: ${res.status} ${res.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = payload?.data;
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
const errors = data.filter((t) => t?.status === "error");
|
||||||
|
if (errors.length) {
|
||||||
|
logger?.log?.("expo-push-ticket-errors", "ERROR", "notifications", "api", { errors });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Build a summary string for push notification body.
|
||||||
* @param count
|
* @param count
|
||||||
* @param jobRoNumber
|
* @param jobRoNumber
|
||||||
* @param bodyShopName
|
* @param bodyShopName
|
||||||
@@ -59,22 +203,17 @@ const buildPushSummary = ({ count, jobRoNumber, bodyShopName }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the FCM notification queues and workers.
|
* Loads the push notification queues and workers (Expo push).
|
||||||
* @param pubClient
|
* @param pubClient
|
||||||
* @param logger
|
* @param logger
|
||||||
* @returns {Promise<Queue<any, any, string, ExtractDataType<any, any>, ExtractResultType<any, any>, ExtractNameType<any, string>>|null>}
|
* @returns {Promise<Queue|null>}
|
||||||
*/
|
*/
|
||||||
const loadFcmQueue = async ({ pubClient, logger }) => {
|
const loadFcmQueue = async ({ pubClient, logger }) => {
|
||||||
if (!hasFirebaseEnv()) {
|
|
||||||
devDebugLogger("FCM queue not initialized (Firebase env not configured).");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fcmAddQueue || !fcmConsolidateQueue) {
|
if (!fcmAddQueue || !fcmConsolidateQueue) {
|
||||||
const prefix = getBullMQPrefix();
|
const prefix = getBullMQPrefix();
|
||||||
const devKey = process.env?.NODE_ENV === "production" ? "prod" : "dev";
|
const devKey = process.env?.NODE_ENV === "production" ? "prod" : "dev";
|
||||||
|
|
||||||
devDebugLogger(`Initializing FCM Queues with prefix: ${prefix}`);
|
devDebugLogger(`Initializing Expo Push Queues with prefix: ${prefix}`);
|
||||||
|
|
||||||
fcmAddQueue = new Queue("fcmAdd", {
|
fcmAddQueue = new Queue("fcmAdd", {
|
||||||
prefix,
|
prefix,
|
||||||
@@ -93,35 +232,40 @@ const loadFcmQueue = async ({ pubClient, logger }) => {
|
|||||||
async (job) => {
|
async (job) => {
|
||||||
const { jobId, jobRoNumber, bodyShopId, bodyShopName, scenarioKey, key, variables, body, recipients } =
|
const { jobId, jobRoNumber, bodyShopId, bodyShopName, scenarioKey, key, variables, body, recipients } =
|
||||||
job.data;
|
job.data;
|
||||||
devDebugLogger(`Adding FCM notifications for jobId ${jobId}`);
|
|
||||||
|
|
||||||
const redisKeyPrefix = `fcm:${devKey}:notifications:${jobId}`;
|
devDebugLogger(`Adding push notifications for jobId ${jobId}`);
|
||||||
|
|
||||||
for (const r of recipients) {
|
const recipientsSetKey = `fcm:${devKey}:recipients:${jobId}`; // set of user emails
|
||||||
|
const metaKey = `fcm:${devKey}:meta:${jobId}`;
|
||||||
|
const redisKeyPrefix = `fcm:${devKey}:notifications:${jobId}`; // per-user list keys
|
||||||
|
|
||||||
|
// store job-level metadata once
|
||||||
|
await pubClient.hsetnx(metaKey, "jobRoNumber", jobRoNumber || "");
|
||||||
|
await pubClient.hsetnx(metaKey, "bodyShopId", bodyShopId || "");
|
||||||
|
await pubClient.hsetnx(metaKey, "bodyShopName", bodyShopName || "");
|
||||||
|
await pubClient.expire(metaKey, seconds(NOTIFICATION_EXPIRATION));
|
||||||
|
|
||||||
|
for (const r of recipients || []) {
|
||||||
|
const user = r?.user;
|
||||||
const associationId = r?.associationId;
|
const associationId = r?.associationId;
|
||||||
if (!associationId) continue;
|
|
||||||
|
|
||||||
const assocKey = `${redisKeyPrefix}:${associationId}`;
|
if (!user) continue;
|
||||||
|
|
||||||
|
const userKey = `${redisKeyPrefix}:${user}`;
|
||||||
const payload = JSON.stringify({
|
const payload = JSON.stringify({
|
||||||
body: body || "",
|
body: body || "",
|
||||||
scenarioKey: scenarioKey || "",
|
scenarioKey: scenarioKey || "",
|
||||||
key: key || "",
|
key: key || "",
|
||||||
variables: variables || {},
|
variables: variables || {},
|
||||||
|
associationId: associationId ? String(associationId) : null,
|
||||||
ts: Date.now()
|
ts: Date.now()
|
||||||
});
|
});
|
||||||
|
|
||||||
await pubClient.rpush(assocKey, payload);
|
await pubClient.rpush(userKey, payload);
|
||||||
await pubClient.expire(assocKey, NOTIFICATION_EXPIRATION / 1000);
|
await pubClient.expire(userKey, seconds(NOTIFICATION_EXPIRATION));
|
||||||
const recipientsSetKey = `fcm:${devKey}:recipients:${jobId}`;
|
|
||||||
await pubClient.sadd(recipientsSetKey, associationId);
|
|
||||||
await pubClient.expire(recipientsSetKey, NOTIFICATION_EXPIRATION / 1000);
|
|
||||||
|
|
||||||
// store some metadata once per jobId
|
await pubClient.sadd(recipientsSetKey, user);
|
||||||
const metaKey = `fcm:${devKey}:meta:${jobId}`;
|
await pubClient.expire(recipientsSetKey, seconds(NOTIFICATION_EXPIRATION));
|
||||||
await pubClient.hsetnx(metaKey, "jobRoNumber", jobRoNumber || "");
|
|
||||||
await pubClient.hsetnx(metaKey, "bodyShopId", bodyShopId || "");
|
|
||||||
await pubClient.hsetnx(metaKey, "bodyShopName", bodyShopName || "");
|
|
||||||
await pubClient.expire(metaKey, NOTIFICATION_EXPIRATION / 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const consolidateKey = `fcm:${devKey}:consolidate:${jobId}`;
|
const consolidateKey = `fcm:${devKey}:consolidate:${jobId}`;
|
||||||
@@ -138,10 +282,11 @@ const loadFcmQueue = async ({ pubClient, logger }) => {
|
|||||||
backoff: LOCK_EXPIRATION
|
backoff: LOCK_EXPIRATION
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
await pubClient.expire(consolidateKey, CONSOLIDATION_KEY_EXPIRATION / 1000);
|
|
||||||
devDebugLogger(`Scheduled FCM consolidation for jobId ${jobId}`);
|
await pubClient.expire(consolidateKey, seconds(CONSOLIDATION_KEY_EXPIRATION));
|
||||||
|
devDebugLogger(`Scheduled consolidation for jobId ${jobId}`);
|
||||||
} else {
|
} else {
|
||||||
devDebugLogger(`FCM consolidation already scheduled for jobId ${jobId}`);
|
devDebugLogger(`Consolidation already scheduled for jobId ${jobId}`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ prefix, connection: pubClient, concurrency: 5 }
|
{ prefix, connection: pubClient, concurrency: 5 }
|
||||||
@@ -154,63 +299,95 @@ const loadFcmQueue = async ({ pubClient, logger }) => {
|
|||||||
const devKey = process.env?.NODE_ENV === "production" ? "prod" : "dev";
|
const devKey = process.env?.NODE_ENV === "production" ? "prod" : "dev";
|
||||||
|
|
||||||
const lockKey = `lock:${devKey}:fcmConsolidate:${jobId}`;
|
const lockKey = `lock:${devKey}:fcmConsolidate:${jobId}`;
|
||||||
const lockAcquired = await pubClient.set(lockKey, "locked", "NX", "EX", LOCK_EXPIRATION / 1000);
|
const lockAcquired = await pubClient.set(lockKey, "locked", "NX", "EX", seconds(LOCK_EXPIRATION));
|
||||||
|
|
||||||
if (!lockAcquired) {
|
if (!lockAcquired) {
|
||||||
devDebugLogger(`Skipped FCM consolidation for jobId ${jobId} - lock held by another worker`);
|
devDebugLogger(`Skipped consolidation for jobId ${jobId} - lock held by another worker`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const admin = getFirebaseAdmin();
|
const recipientsSet = `fcm:${devKey}:recipients:${jobId}`;
|
||||||
if (!admin) {
|
const userEmails = await pubClient.smembers(recipientsSet);
|
||||||
devDebugLogger("FCM consolidation skipped (Firebase not available).");
|
|
||||||
|
if (!userEmails?.length) {
|
||||||
|
devDebugLogger(`No recipients found for jobId ${jobId}`);
|
||||||
|
await pubClient.del(`fcm:${devKey}:consolidate:${jobId}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const recipientsSet = `fcm:${devKey}:recipients:${jobId}`;
|
// Load meta
|
||||||
const associationIds = await pubClient.smembers(recipientsSet);
|
|
||||||
|
|
||||||
const metaKey = `fcm:${devKey}:meta:${jobId}`;
|
const metaKey = `fcm:${devKey}:meta:${jobId}`;
|
||||||
const meta = await pubClient.hgetall(metaKey);
|
const meta = await pubClient.hgetall(metaKey);
|
||||||
const jobRoNumber = meta?.jobRoNumber || "";
|
const jobRoNumber = meta?.jobRoNumber || "";
|
||||||
const bodyShopId = meta?.bodyShopId || "";
|
const bodyShopId = meta?.bodyShopId || "";
|
||||||
const bodyShopName = meta?.bodyShopName || "";
|
const bodyShopName = meta?.bodyShopName || "";
|
||||||
|
|
||||||
for (const associationId of associationIds) {
|
// Fetch tokens for all recipients (1 DB round-trip)
|
||||||
const assocKey = `fcm:${devKey}:notifications:${jobId}:${associationId}`;
|
const usersResp = await gqlClient.request(GET_USERS_FCM_TOKENS_BY_EMAILS, { emails: userEmails });
|
||||||
const messages = await pubClient.lrange(assocKey, 0, -1);
|
const tokenMap = new Map(
|
||||||
|
(usersResp?.users || []).map((u) => [String(u.email), normalizeTokens(u.fcmtokens)])
|
||||||
|
);
|
||||||
|
|
||||||
if (!messages?.length) continue;
|
for (const userEmail of userEmails) {
|
||||||
|
const userKey = `fcm:${devKey}:notifications:${jobId}:${userEmail}`;
|
||||||
|
const raw = await pubClient.lrange(userKey, 0, -1);
|
||||||
|
|
||||||
const count = messages.length;
|
if (!raw?.length) {
|
||||||
|
await pubClient.del(userKey);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = raw
|
||||||
|
.map((x) => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(x);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
const count = parsed.length;
|
||||||
const notificationBody = buildPushSummary({ count, jobRoNumber, bodyShopName });
|
const notificationBody = buildPushSummary({ count, jobRoNumber, bodyShopName });
|
||||||
|
|
||||||
const topic = topicForAssociation(associationId);
|
// associationId should be stable for a user in a job’s bodyshop; take first non-null
|
||||||
|
const associationId =
|
||||||
|
parsed.find((p) => p?.associationId)?.associationId != null
|
||||||
|
? String(parsed.find((p) => p?.associationId)?.associationId)
|
||||||
|
: "";
|
||||||
|
|
||||||
// FCM "data" values MUST be strings
|
const tokens = tokenMap.get(String(userEmail)) || [];
|
||||||
await admin.messaging().send({
|
|
||||||
topic,
|
if (!tokens.length) {
|
||||||
notification: {
|
devDebugLogger(`No Expo push tokens for ${userEmail}; skipping push for jobId ${jobId}`);
|
||||||
title: "ImEX Online",
|
await pubClient.del(userKey);
|
||||||
body: notificationBody
|
continue;
|
||||||
},
|
}
|
||||||
|
|
||||||
|
// Build 1 message per device token
|
||||||
|
const messages = tokens.map((token) => ({
|
||||||
|
to: token,
|
||||||
|
title: "ImEX Online",
|
||||||
|
body: notificationBody,
|
||||||
|
priority: "high",
|
||||||
data: {
|
data: {
|
||||||
type: "job-notification",
|
type: "job-notification",
|
||||||
jobId: String(jobId),
|
jobId: String(jobId),
|
||||||
jobRoNumber: String(jobRoNumber || ""),
|
jobRoNumber: String(jobRoNumber || ""),
|
||||||
bodyShopId: String(bodyShopId || ""),
|
bodyShopId: String(bodyShopId || ""),
|
||||||
bodyShopName: String(bodyShopName || ""),
|
bodyShopName: String(bodyShopName || ""),
|
||||||
associationId: String(associationId),
|
associationId: String(associationId || ""),
|
||||||
|
userEmail: String(userEmail),
|
||||||
count: String(count)
|
count: String(count)
|
||||||
},
|
}
|
||||||
android: { priority: "high" },
|
}));
|
||||||
apns: { headers: { "apns-priority": "10" } }
|
|
||||||
});
|
|
||||||
|
|
||||||
devDebugLogger(`Sent FCM push to topic ${topic} for jobId ${jobId} (${count} updates)`);
|
await sendExpoPush({ messages, logger });
|
||||||
|
|
||||||
await pubClient.del(assocKey);
|
devDebugLogger(`Sent Expo push to ${userEmail} for jobId ${jobId} (${count} updates)`);
|
||||||
|
|
||||||
|
await pubClient.del(userKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
await pubClient.del(recipientsSet);
|
await pubClient.del(recipientsSet);
|
||||||
@@ -241,9 +418,9 @@ const loadFcmQueue = async ({ pubClient, logger }) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const shutdown = async () => {
|
const shutdown = async () => {
|
||||||
devDebugLogger("Closing FCM queue workers...");
|
devDebugLogger("Closing push queue workers...");
|
||||||
await Promise.all([fcmAddWorker.close(), fcmConsolidateWorker.close()]);
|
await Promise.all([fcmAddWorker.close(), fcmConsolidateWorker.close()]);
|
||||||
devDebugLogger("FCM queue workers closed");
|
devDebugLogger("Push queue workers closed");
|
||||||
};
|
};
|
||||||
|
|
||||||
registerCleanupTask(shutdown);
|
registerCleanupTask(shutdown);
|
||||||
@@ -253,7 +430,7 @@ const loadFcmQueue = async ({ pubClient, logger }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the FCM add queue.
|
* Get the add queue.
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
const getQueue = () => {
|
const getQueue = () => {
|
||||||
@@ -262,12 +439,11 @@ const getQueue = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch FCM notifications to the FCM add queue.
|
* Dispatch push notifications to the add queue.
|
||||||
* @param fcmsToDispatch
|
* @param fcmsToDispatch
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
const dispatchFcmsToQueue = async ({ fcmsToDispatch }) => {
|
const dispatchFcmsToQueue = async ({ fcmsToDispatch }) => {
|
||||||
if (!hasFirebaseEnv()) return;
|
|
||||||
const queue = getQueue();
|
const queue = getQueue();
|
||||||
|
|
||||||
for (const fcm of fcmsToDispatch) {
|
for (const fcm of fcmsToDispatch) {
|
||||||
|
|||||||
Reference in New Issue
Block a user