notify and debug apple-signin api
This commit is contained in:
37
pages/api/notify.ts
Normal file
37
pages/api/notify.ts
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
const webPush = require('web-push')
|
||||
|
||||
webPush.setVapidDetails(
|
||||
`mailto:${process.env.WEB_PUSH_EMAIL}`,
|
||||
process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY,
|
||||
process.env.WEB_PUSH_PRIVATE_KEY
|
||||
)
|
||||
|
||||
const Notification = (req, res) => {
|
||||
if (req.method == 'POST') {
|
||||
const { subscription } = req.body
|
||||
|
||||
webPush
|
||||
.sendNotification(
|
||||
subscription,
|
||||
JSON.stringify({ title: 'Hello Web Push', message: 'Your web push notification is here!' })
|
||||
)
|
||||
.then(response => {
|
||||
res.writeHead(response.statusCode, response.headers).end(response.body)
|
||||
})
|
||||
.catch(err => {
|
||||
if ('statusCode' in err) {
|
||||
res.writeHead(err.statusCode, err.headers).end(err.body)
|
||||
} else {
|
||||
console.error(err)
|
||||
res.statusCode = 500
|
||||
res.end()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
res.statusCode = 405
|
||||
res.end()
|
||||
}
|
||||
}
|
||||
|
||||
export default Notification
|
Reference in New Issue
Block a user