Skip to content

: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.

fix(cdp): retry each apns device rejected for its provider token (#118709)

Daniel Marchukfdf23cb8f426
diff --git a/nodejs/src/cdp/services/messaging/push-notification.service.test.ts b/nodejs/src/cdp/services/messaging/push-notification.service.test.ts
index 4cf129ba38e9..1dcda525f2f8 100644
--- a/nodejs/src/cdp/services/messaging/push-notification.service.test.ts
+++ b/nodejs/src/cdp/services/messaging/push-notification.service.test.ts
@@ -1034,7 +1034,7 @@ describe('PushNotificationService', () => {
valkeyStore.set(jwtCacheKey, tokenIssuedMinutesAgo(30))
})
- it('keeps a rejected token that is younger than the refresh window', async () => {
+ it('retries a young rejected token once without replacing it', async () => {
const youngToken = tokenIssuedMinutesAgo(1)
valkeyStore.set(jwtCacheKey, youngToken)
mockTrackedFetch.mockResolvedValue(apnsResponse(403, 'InvalidProviderToken'))
@@ -1042,11 +1042,35 @@ describe('PushNotificationService', () => {
const result = await send()
expect(result.error).toContain('InvalidProviderToken')
- expect(sentTokens()).toHaveLength(1)
+ expect(sentTokens()).toEqual([`bearer ${youngToken}`, `bearer ${youngToken}`])
expect(valkeyStore.get(jwtCacheKey)).toBe(youngToken)
expect([...valkeyStore.keys()].some((key) => key.includes('jwt-refresh'))).toBe(false)
})
+ it('retries a rejected device when another device of the person delivered', async () => {
+ valkeyStore.set(jwtCacheKey, tokenIssuedMinutesAgo(1))
+ let tokenARejected = false
+ mockTrackedFetch.mockImplementation(({ url }: { url: string }) => {
+ if (url.endsWith('/token-a') && !tokenARejected) {
+ tokenARejected = true
+ return Promise.resolve(apnsResponse(403, 'InvalidProviderToken'))
+ }
+ return Promise.resolve(apnsResponse(200))
+ })
+
+ const result = await service.executeSendPushNotification(
+ createSendPushNotificationInvocation({
+ [deviceSubscriptionKey('com.example.app', 'token-a')]: encryptedFields.encrypt('token-a'),
+ [deviceSubscriptionKey('com.example.app', 'token-b')]: encryptedFields.encrypt('token-b'),
+ })
+ )
+
+ expect(result.logs.map((log: any) => log.message)).toContainEqual(
+ expect.stringContaining('accepted by APNs for 2 of 2 device(s)')
+ )
+ expect(mockTrackedFetch).toHaveBeenCalledTimes(3)
+ })
+
it.each(['InvalidProviderToken', 'ExpiredProviderToken'])(
'retries once with a new token after %s',
async (reason) => {
@@ -1074,7 +1098,7 @@ describe('PushNotificationService', () => {
for (const result of results) {
expect(result.error).toContain('InvalidProviderToken')
}
- expect(mockTrackedFetch).toHaveBeenCalledTimes(4)
+ expect(mockTrackedFetch).toHaveBeenCalledTimes(6)
expect(new Set(sentTokens()).size).toBe(2)
expect(mockValkeySet).toHaveBeenCalledWith(
expect.stringContaining('@posthog/apns-provider-jwt-refresh/'),
@@ -1262,7 +1286,7 @@ describe('PushNotificationService', () => {
)
}
- expect(mockTrackedFetch).toHaveBeenCalledTimes(3)
+ expect(mockTrackedFetch).toHaveBeenCalledTimes(6)
expect(new Set(sentTokens()).size).toBe(1)
})
diff --git a/nodejs/src/cdp/services/messaging/push-notification.service.ts b/nodejs/src/cdp/services/messaging/push-notification.service.ts
index bc810e3461d3..616de638f0c7 100644
--- a/nodejs/src/cdp/services/messaging/push-notification.service.ts
+++ b/nodejs/src/cdp/services/messaging/push-notification.service.ts
@@ -647,20 +647,27 @@ export class PushNotificationService {
{ apnsHost, bundleId, jwt, templateId },
addLog
)
- if (!tokenRefreshed && outcome.error?.code && APNS_REJECTED_TOKEN_CODES.has(outcome.error.code)) {
+ // Apple rejects a small share of requests with a token it accepts on the next one. Retry each device
+ // here, because a person whose other device delivered is never rescheduled.
+ if (outcome.error?.code && APNS_REJECTED_TOKEN_CODES.has(outcome.error.code)) {
+ const refreshed = tokenRefreshed ? null : await this.refreshApnsJwt(appleTeamId, keyId, signingKey, jwt)
tokenRefreshed = true
- const refreshed = await this.refreshApnsJwt(appleTeamId, keyId, signingKey, jwt)
if (refreshed) {
jwt = refreshed
- addLog('warn', 'APNs rejected the provider token. Retrying once with a new token.')
- outcome = await this.sendOneApns(
- result,
- params,
- subscription,
- { apnsHost, bundleId, jwt, templateId },
- addLog
- )
}
+ addLog(
+ 'warn',
+ refreshed
+ ? 'APNs rejected the provider token. Retrying once with a new token.'
+ : 'APNs rejected the provider token. Retrying once.'
+ )
+ outcome = await this.sendOneApns(
+ result,
+ params,
+ subscription,
+ { apnsHost, bundleId, jwt, templateId },
+ addLog
+ )
}
if (outcome.sent) {
delivered++