:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cdp): warn about missing push tokens only when no channel delivered (#119289)
posthog[bot]ae1cd6cdf40f
diff --git a/nodejs/src/cdp/services/messaging/push-notification.service.test.ts b/nodejs/src/cdp/services/messaging/push-notification.service.test.tsindex e77fcd707b37..8a2f5e8c636f 100644--- a/nodejs/src/cdp/services/messaging/push-notification.service.test.ts+++ b/nodejs/src/cdp/services/messaging/push-notification.service.test.ts@@ -202,8 +202,11 @@ describe('PushNotificationService', () => {const result = await service.executeSendPushNotification(invocation)- expect(result.logs.map((log) => log.message)).toContainEqual(- expect.stringContaining('No active FCM device token found')+ expect(result.logs).toContainEqual(+ expect.objectContaining({+ level: 'warn',+ message: expect.stringContaining('No active FCM device token found'),+ }))// No token means nothing was delivered — record push_skipped, not push_sent.expect(result.metrics).toContainEqual(expect.objectContaining({ metric_name: 'push_skipped', count: 1 }))@@ -1762,6 +1765,28 @@ describe('PushNotificationService', () => {expect(urls.some((u: string) => u.includes('push.apple.com'))).toBe(true)})+ it('logs a skipped channel at info and reports channel counts when another channel delivered', async () => {+ ;(integrationManager.get as jest.Mock).mockImplementation((id: number) =>+ Promise.resolve(id === 1 ? firebaseIntegration : apnsIntegration)+ )+ const invocation = createSendPushNotificationInvocation({+ '$device_push_subscription_test-project': encryptedFields.encrypt('fcm-token'),+ })+ invocation.queueParameters = { ...invocation.queueParameters, integrationIds: [1, 2] } as any+ mockTrackedFetch.mockResolvedValue(ok200)++ const result = await service.executeSendPushNotification(invocation)++ expect(result.logs).toContainEqual(+ expect.objectContaining({+ level: 'info',+ message: expect.stringContaining('No active APNS device token found'),+ })+ )+ expect(result.logs.filter((log) => log.level === 'warn')).toEqual([])+ expect(result.execResult).toEqual({ delivered: 1, skipped: 1, failed: 0 })+ })+it('deduplicates repeated channel ids so a device is not notified twice', async () => {;(integrationManager.get as jest.Mock).mockResolvedValue(firebaseIntegration)const invocation = createSendPushNotificationInvocation({diff --git a/nodejs/src/cdp/services/messaging/push-notification.service.ts b/nodejs/src/cdp/services/messaging/push-notification.service.tsindex ee19f431d994..6678cabbff96 100644--- a/nodejs/src/cdp/services/messaging/push-notification.service.ts+++ b/nodejs/src/cdp/services/messaging/push-notification.service.ts@@ -352,6 +352,7 @@ export class PushNotificationService {let otherErrorCount = 0const errors: PushSendError[] = []let firstError: string | undefined+ const skipMessages: string[] = []// Which providers took delivery. Only its emptiness is read, to decide whether there is a// delivered notification worth capturing.const deliveredPlatforms = new Set<string>()@@ -400,9 +401,17 @@ export class PushNotificationService {let sent: booleanif (integration.kind === 'firebase') {- sent = await this.executeFcm(result, params, integration, invocation, recipient)+ sent = await this.executeFcm(result, params, integration, invocation, recipient, skipMessages)} else if (integration.kind === 'apns') {- sent = await this.executeApns(result, params, integration, invocation, recipient, apnsAcceptedKeys)+ sent = await this.executeApns(+ result,+ params,+ integration,+ invocation,+ recipient,+ apnsAcceptedKeys,+ skipMessages+ )} else {throw new Error(`Unsupported push integration kind: ${integration.kind}`)}@@ -428,6 +437,12 @@ export class PushNotificationService {}}+ // Most recipients have a device on one platform only, so a skip next to a delivery is expected.+ const skipLogLevel = successCount > 0 ? 'info' : 'warn'+ for (const message of skipMessages) {+ addLog(skipLogLevel, message)+ }+const errorCount = errors.length + otherErrorCountconst success = successCount > 0 || errorCount === 0const retriableErrors = errors.filter((e) => e.retriable)@@ -520,6 +535,9 @@ export class PushNotificationService {}// Surface the error to the hog template too, otherwise its failure message renders blank.result.invocation.state.vmState!.stack.push({ success, error: success ? null : (firstError ?? null) })+ // The template does not resume after this send, so the step's output is set here. It lets a workflow+ // store the channel counts in an output variable and branch on them.+ result.execResult = { delivered: successCount, skipped: skippedCount, failed: errorCount }return result}@@ -567,7 +585,8 @@ export class PushNotificationService {params: CyclotronInvocationQueueParametersSendPushNotificationType,integration: IntegrationType,invocation: CyclotronJobInvocationHogFunction,- recipient: PushRecipient+ recipient: PushRecipient,+ skipMessages: string[]): Promise<boolean> {const addLog = createAddLogFunction(result.logs)@@ -580,7 +599,7 @@ export class PushNotificationService {const subscriptions = getDevicePushSubscriptions(recipient.properties, projectId, this.encryptedFields)if (subscriptions.length === 0) {- addLog('warn', `No active FCM device token found for distinct_id: ${recipient.distinctId}`)+ skipMessages.push(`No active FCM device token found for distinct_id: ${recipient.distinctId}`)pushNotificationSkippedCounter.labels({ platform: 'fcm', reason: 'no_token' }).inc()return false}@@ -699,7 +718,8 @@ export class PushNotificationService {integration: IntegrationType,invocation: CyclotronJobInvocationHogFunction,recipient: PushRecipient,- apnsAcceptedKeys: Set<string>+ apnsAcceptedKeys: Set<string>,+ skipMessages: string[]): Promise<boolean> {const addLog = createAddLogFunction(result.logs)@@ -714,7 +734,7 @@ export class PushNotificationService {const subscriptions = getDevicePushSubscriptions(recipient.properties, bundleId, this.encryptedFields)if (subscriptions.length === 0) {- addLog('warn', `No active APNS device token found for distinct_id: ${recipient.distinctId}`)+ skipMessages.push(`No active APNS device token found for distinct_id: ${recipient.distinctId}`)pushNotificationSkippedCounter.labels({ platform: 'apns', reason: 'no_token' }).inc()return false}