gpt4 book ai didi

javascript - 在 Django 中使用 chrome 实现推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:39 26 4
gpt4 key购买 nike

我学会了使用 chrome 为 Web 应用程序实现推送通知 https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web?hl=en并成功运行博客中提到的示例代码。

不幸的是,我无法用 Django 复制成功。它永远不会进入 service worker 的 ready 方法,(navigator.serviceWorker.ready.then) 即,service worker 永远不会准备好。

根据 http://www.html5rocks.com/en/tutorials/service-worker/introduction/

One subtlety with the register method is the location of the service worker file. You'll notice in this case that the service worker file is at the root of the domain. This means that the service worker's scope will be the entire origin. In other words, this service worker will receive fetch events for everything on this domain. If we register the service worker file at /example/sw.js, then the service worker would only see fetch events for pages whose URL starts with /example/ (i.e. /example/page1/, /example/page2/).

在Django中,如何将一个JS文件放到应用程序的根目录下?目前,服务人员的范围是:http://127.0.0.1:8000/static/ (当我使用 chrome://serviceworker-internals/时)

最佳答案

按照这个方法...

  • sw.js文件放入template文件夹
  • 配置 View 作为静态文件

    #urls
    url(r'^sw(.*.js)$', views.sw_js, name='sw_js'),

    #views
    from django.views.decorators.cache import never_cache
    from django.template.loader import get_template
    @never_cache
    def sw_js(request, js):
    template = get_template('sw.js')
    html = template.render()
    return HttpResponse(html, content_type="application/x-javascript")

关于javascript - 在 Django 中使用 chrome 实现推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34389485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com