Skip to content

Commit 48858c3

Browse files
authored
updated to cover client (#508)
1 parent 6fe43e1 commit 48858c3

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

docs/mini-apps/core-concepts/notifications.mdx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,70 @@ description: Regularly re-engage users by sending in-app notifications through t
55

66
## Overview
77

8-
When a user enables notifications for your Mini App, the Base App generates a unique notification `token` and `url` which is sent to your server via webhook.
8+
When a user enables notifications for your Mini App, the Base app generates a unique notification `token` and `url` which is sent to your server via webhook.
99

1010
This `token` grants your app permission to send in-app notifications to that specific user.
1111

1212
To send a notification, make a `POST` request to the `url` with the user's notification `token` and your content.
1313

1414
You will receive webhook events when users enable or disable notifications for your app. When disabled, the notification token becomes invalid and should no longer be used.
15+
16+
1517
<Panel>
1618
![notification-image-iphone](/images/minikit/notifications-sample.png)
17-
19+
<Info>
20+
Notification tokens are unique to each client app. This means a user can have separate notification preferences for your Mini App across different clients (e.g., Farcaster, the Base app). Removing your Mini App in one client does not affect its status in other clients.
21+
</Info>
1822
</Panel>
1923
## Implementation
2024
<Steps>
2125
<Step title="Create a webhook server">
22-
Create a webhook server to handle webhook events.
26+
Create a webhook server to handle webhook events.
27+
28+
<Info>
29+
The `data` object returned by `parseWebhookEvent` contains three key fields:
30+
- **`fid`**: The user's FID
31+
- **`appFid`**: The client's FID (the Base app is 309857)
32+
- **`event`**: The event payload with type and notification details
33+
34+
Always use both `fid` and `appFid` together to identify a unique user-client combination.
35+
</Info>
2336

2437
```ts app/api/webhook/route.ts expandable highlight={20-50}
38+
39+
import {
40+
parseWebhookEvent,
41+
verifyAppKeyWithNeynar,
42+
} from "@farcaster/miniapp-node";
43+
2544
export async function POST(request: NextRequest) {
2645
const requestJson = await request.json();
2746

2847
// Parse and verify the webhook event
2948
let data;
3049
try {
31-
data = await validateWebhookEventSignature(requestJson);
50+
data = await parseWebhookEvent(requestJson, verifyAppKeyWithNeynar);
3251
// Events are signed by the app key of a user with a JSON Farcaster Signature.
3352
} catch (e: unknown) {
3453
// Handle verification errors (invalid data, invalid app key, etc.)
3554
// Return appropriate error responses with status codes 400, 401, or 500
3655
}
3756

57+
// Extract webhook data
58+
3859
const fid = data.fid;
60+
const appFid = data.appFid; // The FID of the client app that the user added the Mini App to
3961
const event = data.event;
4062

4163
// Handle different event types
4264
switch (event.event) {
4365
case "miniapp_added":
4466
// Save notification details and send welcome notification
4567
if (event.notificationDetails) {
46-
await setUserNotificationDetails(fid, event.notificationDetails);
68+
await setUserNotificationDetails(fid, appFid, event.notificationDetails);
4769
await sendMiniAppNotification({
4870
fid,
71+
appFid,
4972
title: "Welcome to Base Mini Apps",
5073
body: "Mini app is now added to your client",
5174
});
@@ -54,22 +77,23 @@ You will receive webhook events when users enable or disable notifications for y
5477

5578
case "miniapp_removed":
5679
// Delete notification details
57-
await deleteUserNotificationDetails(fid);
80+
await deleteUserNotificationDetails(fid, appFid);
5881
break;
5982

6083
case "notifications_enabled":
6184
// Save new notification details and send confirmation
62-
await setUserNotificationDetails(fid, event.notificationDetails);
85+
await setUserNotificationDetails(fid, appFid, event.notificationDetails);
6386
await sendMiniAppNotification({
6487
fid,
88+
appFid,
6589
title: "Ding ding ding",
6690
body: "Notifications are now enabled",
6791
});
6892
break;
6993

7094
case "notifications_disabled":
7195
// Delete notification details
72-
await deleteUserNotificationDetails(fid);
96+
await deleteUserNotificationDetails(fid, appFid);
7397
break;
7498
}
7599

@@ -161,14 +185,16 @@ You will receive webhook events when users enable or disable notifications for y
161185
```ts sendNotification.ts highlight={15-28}
162186
export async function sendMiniAppNotification({
163187
fid,
188+
appFid,
164189
title,
165190
body,
166191
}: {
167192
fid: number;
193+
appFid: number;
168194
title: string;
169195
body: string;
170196
}): Promise<sendMiniAppNotificationResult> {
171-
const notificationDetails = await getUserNotificationDetails(fid);
197+
const notificationDetails = await getUserNotificationDetails(fid, appFid);
172198
if (!notificationDetails) {
173199
return { state: "no_token" };
174200
}
@@ -263,7 +289,7 @@ Mini App events use the following object structure:
263289

264290
* **`type`**: notification event type
265291
* **`notificationDetails.url`**: URL that the app should call to send a notification.
266-
* **`notificationDetails.token`**: A secret token generated by the Base App and shared with the Notification Server. A token is unique for each (Farcaster Client, Mini App, user Fid) tuple.
292+
* **`notificationDetails.token`**: A secret token generated by the Base app and shared with the Notification Server. A token is unique for each (Farcaster Client, Mini App, user Fid) tuple.
267293

268294
<Note>If users are not seeing the option to enable notifications when they call `addMiniApp()`, verify that your manifest file contains a valid `webhookUrl`.</Note>
269295

0 commit comments

Comments
 (0)