Skip to content

Commit d8537f5

Browse files
Expand notification api with reference and more events (#168)
* Expand notification api with reference and more events * Ensure unique identifier
1 parent 346a442 commit d8537f5

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

resources/js/electron-plugin/src/server/api/notification.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ router.post('/', (req, res) => {
1818
actions,
1919
closeButtonText,
2020
toastXml,
21-
event: customEvent
21+
event: customEvent,
22+
reference,
2223
} = req.body;
2324

2425
const eventName = customEvent ?? '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked';
2526

27+
const notificationReference = reference ?? (Date.now() + '.' + Math.random().toString(36).slice(2, 9));
28+
2629
const notification = new Notification({
2730
title,
2831
body,
@@ -42,13 +45,50 @@ router.post('/', (req, res) => {
4245
notification.on("click", (event) => {
4346
notifyLaravel('events', {
4447
event: eventName || '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked',
45-
payload: JSON.stringify(event)
48+
payload: {
49+
reference: notificationReference,
50+
event: JSON.stringify(event),
51+
},
52+
});
53+
});
54+
55+
notification.on("action", (event, index) => {
56+
notifyLaravel('events', {
57+
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationActionClicked',
58+
payload: {
59+
reference: notificationReference,
60+
index,
61+
event: JSON.stringify(event),
62+
},
63+
});
64+
});
65+
66+
notification.on("reply", (event, reply) => {
67+
notifyLaravel('events', {
68+
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationReply',
69+
payload: {
70+
reference: notificationReference,
71+
reply,
72+
event: JSON.stringify(event),
73+
},
74+
});
75+
});
76+
77+
notification.on("close", (event) => {
78+
notifyLaravel('events', {
79+
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationClosed',
80+
payload: {
81+
reference: notificationReference,
82+
event: JSON.stringify(event),
83+
},
4684
});
4785
});
4886

4987
notification.show();
5088

51-
res.sendStatus(200);
89+
res.status(200).json({
90+
reference: notificationReference,
91+
});
5292
});
5393

5494
export default router;

0 commit comments

Comments
 (0)