Push notifications

Open in

Push notifications let you notify users when an agent completes a long-running task, even if the user has closed the app. This uses Ably's native Push Notifications alongside AI Transport.

The pattern

The flow works as follows:

  1. A user sends a message that triggers a long-running agent task (research, code generation, data analysis).
  2. The user closes the app or navigates away.
  3. The agent completes the task and publishes the result to the session channel.
  4. A push rule on the channel triggers a notification to the user's device.
  5. The user taps the notification and returns to the completed conversation.

Because AI Transport persists messages on the Ably channel, the full response is available when the user returns. There is no need to store results separately.

Set up push rules

Configure a channel rule in the Ably dashboard to trigger push notifications when the agent publishes a completion event:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

// Server: publish a completion event when the agent finishes
app.post('/api/chat', async (req, res) => {
  const { turnId, clientId, userId, sessionId, messages } = req.body
  const turn = transport.newTurn({ turnId, clientId })

  const result = streamText({
    model: openai('gpt-4o'),
    messages,
    abortSignal: turn.abortSignal,
  })

  await turn.streamResponse(result.toUIMessageStream())
  await turn.end('complete')

  // Publish a push-eligible event on a notification channel
  const notificationChannel = ably.channels.get(`notifications:${userId}`)
  await notificationChannel.publish('agent-complete', {
    title: 'Your agent has finished',
    body: 'Tap to view the response',
    sessionId,
  })
  res.json({ ok: true })
})

On the client, register the device for push notifications and subscribe to the notification channel:

JavaScript

1

2

3

// Client: subscribe for push notifications
const notificationChannel = ably.channels.get(`notifications:${userId}`)
await notificationChannel.push.subscribeDevice()

Combine with AI Transport sessions

The notification payload can include the session ID, so the client opens directly to the right conversation. When the user taps the notification, load the session and use history and replay to display the full conversation including the completed response.