github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/docs/notifications.md (about) 1 [Table of contents](README.md#table-of-contents) 2 3 # Notifications 4 5 Cozy applications can send notifications to the user, in order to alert or 6 notify for services message or state change that could be of interest to the 7 user, in an asynchronous manner. 8 9 These notifications are part of a "Notification Center" where the user can 10 configure the behavior of these notifications and the channel in which they are 11 sent. 12 13 14 ## Firebase configuration 15 16 The push notifications can be sent via Firebase to smartphones. By default, the 17 key used to connect to firebase is set in the configuration file, via the 18 `notifications.android_api_key` parameter. But it can be useful to have several 19 firebase accounts when there are several applications developed by several 20 organizations. In that case, it is possible to tell the stack to use a particular 21 key for a given app by creating a CouchDB document inside the 22 `secrets/io-cozy-account_types` database, like this: 23 24 ```json 25 { 26 "_id": "myapp", 27 "slug": "myapp", 28 "android_api_key": "th3_f1r3b4s3_k3y" 29 } 30 ``` 31 32 And we can go further to restrict this to a context, by prefixing the `_id` 33 with the context name and `/`: 34 35 ```json 36 { 37 "_id": "mycontext/myapp", 38 "slug": "myapp", 39 "android_api_key": "th3_0th3r_f1r3b4s3_k3y" 40 } 41 ``` 42 43 ## Declare application's notifications 44 45 Each application have to declare in its manifest the notifications it needs to 46 send. The `notifications` fields of the manifest can be used to define all these 47 notifications, with the following properties: 48 49 - `collapsible` (boolean): defines a notification category for which only the 50 last value is of interest to the user. For instance, a account-balance quota 51 for a user's bank account: such notification is only useful as its last 52 value. 53 - `stateful` (boolean): defines a notification storing a piece of state: for 54 each new notification, the stack will check that if the last sent notification 55 has a different state before sending it. If not, the notification will not 56 be resent 57 - `multiple` (boolean): specify the possibility for a notification to have 58 different sub-categories, defined by a programmable/dynamic identifier. 59 `collapsible` and `stateful` properties are inherited for each sub- 60 categories. 61 - `default_priority`: default priority to use, with values "high" or "normal". 62 This is propagated to the underlying mobile notifications system. 63 - `templates`: a link list to templates file contained in the application 64 folder that can be used to write the content of the notification, depending 65 on the communication channel. 66 67 In this documentation, we take the example of an application with the following 68 notification: 69 70 ```json 71 { 72 "notifications": { 73 "account-balance": { 74 "description": "Alert the user when its account balance is negative", 75 "collapsible": true, // only interested in the last value of the notification 76 "multiple": true, // require sub-categories for each account 77 "stateful": true, // piece of state to distinguish notifications 78 "default_priority": "high", // high priority for this notification 79 "templates": { 80 "mail": "file:./notifications/account-balance-mail.tpl" 81 } 82 } 83 } 84 } 85 ``` 86 87 For mobile application, there are two cases: 88 89 - if the mobile application is linked to a web app, it will use the same 90 declaration of notifications from the manifest of the web app 91 - else, the mobile app will need to declare its notifications when 92 [registering its OAuth client](https://docs.cozy.io/en/cozy-stack/auth/#post-authregister). 93 94 ## Creating a notification 95 96 ### POST /notifications 97 98 This endpoint can be used to push a new notification to the user. 99 100 Notifications fields are: 101 102 - `category` (string): name of the notification category 103 - `category_id` (string): name of the notification sub-category if relevant (optional) 104 - `title` (string): title of the notification 105 - `message` (string): message of of the notification (optional) 106 - `priority` (string): priority of the notification (`high` or `normal`), sent 107 to the underlying channel to prioritize the notification 108 - `state` (string): state of the notification. Only needed if your 109 notification is `stateful`, to distinguish notifications 110 - `preferred_channels` (array of string): to select a list of preferred 111 channels for this notification: either `"mobile"`, `"sms"` or `"mail"`. The 112 stack may chose another channels. `["mobile", "mail"]` means that the stack 113 will first try to send a mobile push notification, and if it fails, it will 114 try by mail 115 - `data` (map): key/value map used to create the notification from its 116 template, or sent in the notification payload for mobiles 117 - `at` (string): send the notification later, at this date formatted in 118 ISO-8601 (optional) 119 120 Note that if you send a notification by sms, only the `message` attribute 121 will be sent. Also, keep in mind that, depending on your sms provider, the length 122 of the message cannot be longer than 160 characters. 123 124 #### Request 125 126 ```http 127 POST /notifications HTTP/1.1 128 Host: alice.cozy.localhost 129 Authorization: Bearer ... 130 Content-Type: application/vnd.api+json 131 ``` 132 133 ```json 134 { 135 "data": { 136 "attributes": { 137 "category": "account-balance", 138 "category_id": "my-bank", 139 "title": "Your account balance is not OK", 140 "message": "Warning: we have detected a negative balance in your my-bank", 141 "priority": "high", 142 "state": "-1", 143 "preferred_channels": ["mobile"], 144 "data": { 145 "key1": "value1", 146 "key2": "value2" 147 } 148 } 149 } 150 } 151 ``` 152 153 #### Response 154 155 ```http 156 HTTP/1.1 201 Created 157 Content-Type: application/vnd.api+json 158 ``` 159 160 ```json 161 { 162 "data": { 163 "type": "io.cozy.notifications", 164 "id": "c57a548c-7602-11e7-933b-6f27603d27da", 165 "meta": { 166 "rev": "1-1f2903f9a867" 167 }, 168 "attributes": { 169 "source_id": "cozy/app/bank/account-balance/my-bank", 170 "originator": "app", 171 "slug": "bank", 172 "category": "account-balance", 173 "category_id": "my-bank", 174 "title": "Your account balance is not OK", 175 "message": "Warning: we have detected a negative balance in your my-bank", 176 "priority": "high", 177 "state": "-1", 178 "data": { 179 "key1": "value1", 180 "key2": "value2" 181 } 182 } 183 } 184 } 185 ```