- I have a React application using NextJS, deployed on Vercel.
- I want to use OneSignal to send notifications
I have a certain feature in my app called "Reminders", which is just a MySQL table using the Prisma ORM. Here is the table schema:
model Tasks {
id Int @id @default(autoincrement())
name String
note String?
frequency String
on String
lastDone DateTime
property Property? @relation(fields: [propertyId], references: [id])
propertyId String?
}
- I have already set up an API endpoint called
/api/property/tasks
(POST) which returns the current task list- The API requires a token in the body, which is stored via a cookie called
sessionToken
(which never expires) - A variable called
nextDue
is already calculated based on thefrequency
andon
values. (This returns anew Date()
)
- The API requires a token in the body, which is stored via a cookie called
- Using Service Workers and the OneSignal API, how do I make my app send notifications to the user's device when the device's time is equal to
nextDate
, even if the browser tab is closed? - The service worker will need to update to the new data to send the correct push notification in case the task gets deleted or the date gets updated
Thank you!
EDIT: I'm OK with a solution without using the OneSignal API preferably.
- I have a React application using NextJS, deployed on Vercel.
- I want to use OneSignal to send notifications
I have a certain feature in my app called "Reminders", which is just a MySQL table using the Prisma ORM. Here is the table schema:
model Tasks {
id Int @id @default(autoincrement())
name String
note String?
frequency String
on String
lastDone DateTime
property Property? @relation(fields: [propertyId], references: [id])
propertyId String?
}
- I have already set up an API endpoint called
/api/property/tasks
(POST) which returns the current task list- The API requires a token in the body, which is stored via a cookie called
sessionToken
(which never expires) - A variable called
nextDue
is already calculated based on thefrequency
andon
values. (This returns anew Date()
)
- The API requires a token in the body, which is stored via a cookie called
- Using Service Workers and the OneSignal API, how do I make my app send notifications to the user's device when the device's time is equal to
nextDate
, even if the browser tab is closed? - The service worker will need to update to the new data to send the correct push notification in case the task gets deleted or the date gets updated
Thank you!
EDIT: I'm OK with a solution without using the OneSignal API preferably.
Share Improve this question edited Oct 9, 2022 at 19:46 Manu G asked Oct 7, 2022 at 2:19 Manu GManu G 1735 silver badges14 bronze badges 2- You can try this dev.to/onesignal/… – Divyessh Commented Oct 10, 2022 at 19:09
- Hi! Thank you for your ment, however, the article you sent does not explain how to send push notifications directly from the app itself. My app needs to send push notifications automatically at a given time to users – Manu G Commented Oct 11, 2022 at 17:21
2 Answers
Reset to default 4 +50I guess you are looking for two capabilities of service workers (learn about all available capabilities here):
- Periodic Background Sync API
- Notifications API
You can follow these two reference guides by Microsoft:
- Use the Periodic Background Sync API to regularly get fresh content
- Display notifications in the action center
Do not confuse yourself between Push API & Notifications API. You are only looking for notification functionality not push.
I know this is late but for anyone in the future I have a working example here. it uses the web-push library along with next-pwa.
code: https://github./david-randoll/push-notification-nextjs
live demo: https://push-notification.davidrandoll./
One iPhones, you need to Add to Home first b4 it works.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745615664a4636220.html
评论列表(0条)