|
| 1 | +# Record User Acknowledgements |
| 2 | + |
| 3 | +The extension stores notices and acknowledgements as documents in a collection inside Firestore. You configure the collection during the installation process, when you set up the extension instance's configuration parameters. |
| 4 | + |
| 5 | +The extensions' source code contains a [web app providing admin utilities](https://github.com/invertase/firebase-extensions/tree/main/extensions/firestore-record-user-acknowledgements/admin-dashboard) that you can run locally. This contains a notice builder UI that you can use to easily create and customize notices. Setup instructions are available in the above link provided. Once set up, navigate to [localhost:3000](http://localhost:3000) to see the notice builder tool. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Retrieving a notice |
| 10 | + |
| 11 | +After creating a notice, you can use the following snippet in your web/mobile app to get the notice you want to show to your users, using the notice `type`: |
| 12 | + |
| 13 | +```js |
| 14 | +import { getFunctions, httpsCallable } from 'firebase/functions'; |
| 15 | + |
| 16 | +const functions = getFunctions(); |
| 17 | +const notice = await httpsCallable( |
| 18 | + functions, |
| 19 | + 'ext-firestore-record-user-acknowledgements-getNotice', |
| 20 | +)({ |
| 21 | + type: 'banner', |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +Note: if multiple instances of this extension are installed the callable function name might contain a unique identifier, e.g. `ext-firestore-record-user-acknowledgements-<id>-getNotice`. |
| 26 | + |
| 27 | +The response of the function call will contain the notice document data, along with whether the current authenticated user has acknowledged the notice. |
| 28 | + |
| 29 | +To retrieve a notice by a specific version, provide the `version` parameter: |
| 30 | + |
| 31 | +```js |
| 32 | +import { getFunctions, httpsCallable } from 'firebase/functions'; |
| 33 | + |
| 34 | +const functions = getFunctions(); |
| 35 | +const notice = await httpsCallable( |
| 36 | + functions, |
| 37 | + 'ext-firestore-record-user-acknowledgements-getNotice', |
| 38 | +)({ |
| 39 | + type: 'banner', |
| 40 | + version: 2, |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +View the [reference API](/record-user-acknowldegements/reference) for full details on the response interface. |
| 45 | + |
| 46 | +## Acknowledging a notice |
| 47 | + |
| 48 | +To acknowledge a notice, the extension provides two callable functions which accept the notice ID: `acknowledgeNotice` & `unacknowledgeNotice`. The extension will keep historical records of each acknowledgement the callable functions are called multiple times for the same user and notice. |
| 49 | + |
| 50 | +For example, to acknowledge a notice: |
| 51 | + |
| 52 | +```js |
| 53 | +import { getFunctions, httpsCallable } from "firebase/functions"; |
| 54 | + |
| 55 | +const functions = getFunctions(); |
| 56 | +await httpsCallable(functions, 'ext-firestore-record-user-acknowledgements-acknowledgeNotice)({ |
| 57 | + noticeId: 'EA9QhZKKta9KXcckiasc', |
| 58 | +}); |
| 59 | +``` |
| 60 | +
|
| 61 | +In-case you need to capture custom preferences relating to an acknowledgement, you can provide custom metadata to the function, for example: |
| 62 | +
|
| 63 | +```js |
| 64 | +await httpsCallable(functions, 'ext-firestore-record-user-acknowledgements-acknowledgeNotice)({ |
| 65 | + noticeId: 'EA9QhZKKta9KXcckiasc', |
| 66 | + metadata: { preference1: true, preference2: false }, |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +You can also provide a custom “type” of acknowledgement (the default type is “seen”), for example: |
| 71 | + |
| 72 | +```js |
| 73 | +await httpsCallable(functions, 'ext-firestore-record-user-acknowledgements-acknowledgeNotice)({ |
| 74 | + noticeId: 'EA9QhZKKta9KXcckiasc', |
| 75 | + type: 'seen', |
| 76 | +}); |
| 77 | +``` |
| 78 | +
|
| 79 | +If you wish to unacknowledge a notice, call the `unacknowledgeNotice` function: |
| 80 | +
|
| 81 | +```js |
| 82 | +await httpsCallable(functions, 'ext-firestore-record-user-acknowledgements-unacknowledgeNotice)({ |
| 83 | + noticeId: 'EA9QhZKKta9KXcckiasc', |
| 84 | + metadata: { reason: '...' }, |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +## Retrieving acknowledgements |
| 89 | + |
| 90 | +To retrieve all previous user notice acknowledgements, call the `getAcknowledgements` callable function. This function will return an ordered array of all acknowledgements along with the notice data: |
| 91 | + |
| 92 | +```js |
| 93 | +import { getFunctions, httpsCallable } from "firebase/functions"; |
| 94 | + |
| 95 | +const functions = getFunctions(); |
| 96 | +const acknowledgements = await httpsCallable(functions, 'ext-firestore-record-user-acknowledgements-getAcknowledgements)(); |
| 97 | +``` |
| 98 | +
|
| 99 | +By default this won’t include unacknowledgement documents, however if those are required you can provide a `includeUnacknowledgements` property to the call: |
| 100 | +
|
| 101 | +```js |
| 102 | +import { getFunctions, httpsCallable } from "firebase/functions"; |
| 103 | +
|
| 104 | +const functions = getFunctions(); |
| 105 | +const acknowledgements = await httpsCallable(functions, 'ext-firestore-record-user-acknowledgements-getAcknowledgements)({ |
| 106 | + includeUnacknowledgements: true, |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +## User specific acknowledgements |
| 111 | + |
| 112 | +You may need to create custom notices that are shown to and can only be acknowledged by a specific subset of users. To accomplish this, specify an `allowList` array of UIDs to the notice document. If the user requesting a notice by type is not within the list of UIDs, a `not-found` error will be returned. |
| 113 | + |
| 114 | +## Updating a notice |
| 115 | + |
| 116 | +When it's time to update a notice, for example when additional user preferences are required, create a new notice document with the same `type` as the existing notice you wish to update. |
| 117 | + |
| 118 | +When the notice is retrieved, a new notice document will be returned with no user acknowledgement, allowing users to acknowledge the newer notice. |
0 commit comments