0

我正在使用 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,
4

1 回答 1

1

Prismic allows you set webhooks that trigger when a document is published. See prismic blog. Using express, I created one that will do two things:

  1. Get all blog post from prismic
  2. Send onesignal web push notification when post is published

See code snippet here: https://gitlab.com/-/snippets/2003202

References:

于 2020-08-09T03:36:57.633 回答