我正在使用 Nuxtjs 构建一个 PWA,它从prismic api获取博客内容。OneSignal 已按照此处提供的文档安装和配置,我能够为用户订阅该应用程序并通过 OneSignal 的仪表板提供欢迎推送以及其他推送。
我现在想在新内容发布到博客时发送推送通知。任何帮助,将不胜感激。
编辑
每当用户访问https://example.com/blog时,我都会触发推送通知。注意:棱镜按最新帖子排序,因此this.docs[0]
从数组中获取最新文章。
async fetch() {
try {
const query = await this.$prismic.api.query(this.$prismic.predicates.at('document.type', 'blog_posts'), {pageSize: 6}).then((query)=>{
this.docs = query.results;
const requestOptions = {
method: "POST",
headers: {"Content-Type": "application/json", "Authorization": `Basic ${process.env.API_KEY}`},
body: JSON.stringify({
app_id: process.env.APP_ID,
included_segments: ["All"],
contents: {en: this.docs[0].data.post_content[0].text},
headings: {en: this.docs[0].data.post_title[0].text},
chrome_web_image: this.docs[0].data.featured_image.url,
big_picture: this.docs[0].data.featured_image.url,
web_url: `https://example.com/blog/${this.docs[0].uid}`
})
}; fetch('https://onesignal.com/api/v1/notifications', requestOptions)
})
} catch (e) {
// Send to bugsnag
console.log(e)
}
}, fetchDelay: 500,