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.
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
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.{ 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 akind:
background— runs without opening your app. The press goes to your backend, which does the work. The buttons in the generic sets are allbackground, except Reply.reply— abackgroundbutton that also carries the text the user typed.open— opens your app, at the button’s own path if it has one.
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 youschedule 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.
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:The notification’s buttons, labelled in the user’s language.
Set once a button was pressed, on any device.
message is what your backend answered.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:
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 abackground 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:
{ "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 thenotificationTypes field of your cskn.json. Each deploy publishes the declaration:
The type’s key is lowercase letters, digits and
_, up to 32 characters. Declare at most 16 types.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 withgetNotificationPrefs and change one with setNotificationPref:
Whether your app may notify this user at all. Only the user can change it, in the shell’s notification settings.
Your declared types, in
cskn.json order. title and description are what you declared, so pick the user’s language yourself.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
What the notifications have in common, up to 200 characters. It is scoped to your app and the recipient.
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.
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.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
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.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.