Skip to content

Push Notifications

Jens Alfke edited this page May 30, 2013 · 1 revision

The Sync Gateway should be able to send push notifications to client apps when appropriate, so the user can be alerted of new data even if the app isn't running.

Platforms

iOS (and OS X)

The Apple Push Notification Service (APNS) lets servers send notifications to client apps. Both the app and server need to configure certificates to let them plug into APNS.

At launch time, the app requests a device token (asynchronously). When the token arrives, it needs to pass it to the server. The server can then use this token when sending a notification to the APNS server, to deliver it to that device.

APNS can also tell the app server about tokens that have expired or been revoked, so the server can remove them from its records.

Android

Google Cloud Messaging.

The architecture looks very similar to iOS, so I imagine we can use the same design to support both, probably with some kind of namespacing to identify which platform a device token string belongs to.

API

Registering Device Tokens

An app needs to send its current token to the server, probably as part of starting a replication. We can define a URL for the app to POST to, something like /$DB/_pushtoken.

The server-side handler for this needs to add that token to the authenticated user's data. There can be a new property of the user object, like pushtokens, containing an array of these. (There will be one for every device that the user syncs.)

When APNS tells us about an expired token, we'll either need to hunt it down and remove it from its user document, or just add it to a list and then remove it the next time we post notifications for that user.

Defining When To Notify

The developer needs to be able to configure which types of document changes will trigger notifications, and for which users.

The simplest approach might be to have the developer define a set of channels that trigger notifications (perhaps by giving a regular expression for their names to match?) When a document revision occurs in a matching channel, we then post a notification to every user with access to the channel.

A more flexible approach would be to define a new JavaScript function, like notify(user,message), that can be called from an app's sync function.

Clone this wiki locally