Skip to main content

Permission

requestPermission() resolves true when the user allows notifications. It must be called from a user gesture such as a click.
  • In a shell, the shell asks the operating system, and the device’s push token does the rest.
  • In a browser, it subscribes this browser to Web Push for your app’s origin. See Web Push for what that can and cannot do, notably on iOS.

Local notifications

schedule(payload) resolves { id } on success. Keep the id to cancel it later.
string
required
Notification title.
string
Notification text.
number | null
Delay before it fires. null fires immediately.
boolean
default:"false"
Repeat at the same interval.
Record<string, any>
Payload delivered with the notification.
boolean
default:"true"
Play the notification sound.
{ set: string; ctx?: Record<string, string | number | boolean> }
Buttons on the notification. See Buttons.
cancel(id) cancels one; cancelAll() cancels all of your app’s scheduled notifications.
Scheduling is shell-only. A web page has no dependable way to ask the system to wake it at a given time, so in a browser schedule resolves { error: 'unsupported' }. Send the notification from your server instead.

Badge

In a browser the badge only shows on an installed web app, since a tab has no icon to badge.

Inbox

The inbox lists the notifications the platform delivered to the user from your app. The shell scopes it to your app, so you never see another app’s notifications.
Each item is { id, senderSlug, title, body?, data?, read, createdAt, actions?, actionResult? }. When data holds { path, params }, tapping the notification opens that route in your app. actions and actionResult are described under Buttons. In a browser the inbox is always empty.

Buttons

A notification can carry up to three buttons. Pick a set of buttons, and pass a small context your handler gets back when one is pressed:

Sets

These sets are available to every app. Their labels are translated for the user. Your app can also declare its own sets with its own labels. Ask the cskn team to register them for your app.

Kinds of button

Every button has a kind:
  • background — runs without opening your app. The press goes to your backend, which does the work. The buttons in the generic sets are all background, except Reply.
  • reply — a background button that also carries the text the user typed.
  • open — opens your app, at the button’s own path if it has one.
A mini app cannot run while it is closed. That is why background and reply presses go to your backend, not to your page. See Handling a press.

Where buttons show

A notification you send from your server also shows its buttons in the inbox. If a press cannot be completed (the button expired, the user switched your app off, the device is offline), the shell opens your app instead.

Local notifications

When you schedule a notification with actions, the shell asks the platform for the buttons before it arms the notification.
  • If that fails (for example, the device is offline), the notification is still armed, without buttons.
  • Buttons keep working until a day after the notification fires. So schedule at most a week ahead.
  • Avoid buttons on a notification with repeats. Its later firings would carry expired buttons.

Knowing which button opened your app

onNotificationAction calls you with the id of the button that opened your app. It fires for an open button, and for a reply button in a browser.
The shell passes the id as the cskn_action URL parameter. In a browser it is removed from the address bar once read, so a reload does not repeat it. background buttons never reach your page.

Buttons in your own inbox

Inbox items carry the buttons they were sent with, and what happened to them:
{ id, kind, title, path?, destructive?, authRequired? }[]
The notification’s buttons, labelled in the user’s language.
{ actionId, message?, at }
Set once a button was pressed, on any device. message is what your backend answered.
Press a background button from your own UI with respondToNotification. It works the same as a press on the notification, and runs your handler only once per notification:
It rejects with 410 when the button has expired (a week after the notification), 403 when the user switched your app off, and 502 when your backend failed.

Handling a press

When a background or reply button is pressed, the platform sends a POST request to your app’s action webhook. Until your app has a webhook registered with the cskn team, notifications with these buttons are sent without them, and schedule arms them without buttons. The request looks like this:
Two headers let you check the request comes from the platform:
string
Unix seconds when the request was signed.
string
Hex HMAC-SHA256 of <timestamp>.<raw body>, keyed with your app’s notification secret.
Recompute the signature, compare it in constant time, and reject old timestamps. Answer within five seconds. Answer { "message": "RSVP saved" } to show that text in the inbox. A 5xx response is retried once; a 4xx response is not. Each notification’s button is handled once. If the user presses it again, on another device, the platform answers with your first response and does not call you again.

Notification types

Users can switch off one kind of notification from your app and keep the rest, for example likes but not comments. Declare your kinds in the notificationTypes field of your cskn.json. Each deploy publishes the declaration:
object
The type’s key is lowercase letters, digits and _, up to 32 characters. Declare at most 16 types.
string | Record<locale, string>
required
What the settings row says. Up to 40 characters.
string | Record<locale, string>
A line under the title. Up to 120 characters.
Name the type in every notification your backend sends, in its type field. When the user has switched that type off, the notification is not sent. A type your deployed cskn.json does not declare is refused with 400 UNKNOWN_NOTIFICATION_TYPE. Every type is on until the user switches it off. With two or more types, your app gets its own screen in the shell’s notification settings: your app’s switch at the top, then one switch per type.
cskn.json is the only source. If you remove notificationTypes from it, the next deploy removes your types and the settings screen stops listing them.

Your own settings page

To show the same switches on your own settings page, read them with getNotificationPrefs and change one with setNotificationPref:
boolean
Whether your app may notify this user at all. Only the user can change it, in the shell’s notification settings.
{ key, title, description?, enabled }[]
Your declared types, in cskn.json order. title and description are what you declared, so pick the user’s language yourself.
Both work the same in a shell and in a browser. setNotificationPref rejects with 400 UNKNOWN_NOTIFICATION_TYPE for a type you have not declared.

Grouping

Ten people liking one post should be one notification, not ten. Your backend can ask for that in the notification it sends, in one of two ways.

Fold into one notification

string
required
What the notifications have in common, up to 200 characters. It is scoped to your app and the recipient.
string
required
Who did it this time. A second send from the same actor is not counted again, so unliking and liking again is still one like.
string
The title once more than one actor is in the group. {{count}} is the number of actors and {{others}} is that number minus one. Leave it out to keep title.
string
The same for the body.
While the recipient has not read it, every send with the same key updates one inbox item: its text, its count and its place at the top. The item stays in the inbox until it is read; the next send after that starts a new one. The device is interrupted for the first send, then at most once every five minutes. That push replaces the notification already on screen. The sends in between update only the inbox. A group needs the inbox, so group is ignored with inbox: false.

Replace on screen

string
Up to 200 characters, scoped to your app and the recipient. Every send still pushes, but it takes the place of the last notification with the same key instead of appearing beside it. Nothing is counted for you, so put the count in your own text ("Ali (3 new messages)"). Works with inbox: false.
Use collapseKey for things the user should hear about each time, such as messages in a conversation. Use group for things that add up, such as likes or new followers.
Replacing on screen works in browsers, on Android and on iOS. On older versions of the cskn app the notifications appear side by side instead, but are still grouped in the inbox.

Functions

requestNotificationPermission, scheduleNotification, cancelNotification, cancelAllNotifications, getBadgeCount, setBadgeCount, getNotificationInbox, markInboxRead, onNotificationAction, respondToNotification, getNotificationPrefs, setNotificationPref.