javascript – 在Django中使用chrome实现推送通知
作者:互联网
我学会了使用chrome https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web?hl=en为Web应用程序实现推送通知,并成功运行了博客中提到的示例代码.
不幸的是,我无法用Django复制成功.它永远不会进入服务工作者的就绪方法(navigator.serviceWorker.ready.then),即服务工作者从未准备好.
按照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文件放在模板文件夹中
>将视图配置为静态文件
#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,google-chrome,push-notification,service-worker 来源: https://codeday.me/bug/20190824/1706831.html