在 Django 中使用 chrome 实现推送通知 [英] Implementing push notification using chrome in Django

查看:24
本文介绍了在 Django 中使用 chrome 实现推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have learned to implement push notifications for a Web Application using chrome https://developers.google.com/web/updates/2015/03/push-notifications-on-the-open-web?hl=en and successfully ran the sample code mentioned in the blog.

不幸的是,我无法用 Django 复制成功.它永远不会进入服务工作者的就绪方法,(navigator.serviceWorker.ready.then) 即服务工作者永远不会准备好.

Unfortunately, I couldn't replicate the success with Django. It never goes into the ready method of the service worker,(navigator.serviceWorker.ready.then) ie, the service worker is never ready.

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

注册方法的一个微妙之处是服务的位置工人档案.在这种情况下,您会注意到 service worker 文件是域的根.这意味着服务工作者的范围将是整个原点.换句话说,这个服务工作者将接收此域上所有内容的 fetch 事件.要是我们在/example/sw.js 注册服务工作者文件,然后服务worker 只会看到 URL 开头为的页面的 fetch 事件/example/(即/example/page1/、/example/page2/).

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 文件放在应用程序的根目录下?目前,service worker 的作用域是:http://127.0.0.1:8000/static/ (当我使用 chrome://serviceworker-internals/)

In Django, how to put a JS a file under root of the application? Currently,scope of the service worker is: http://127.0.0.1:8000/static/ (When I use chrome://serviceworker-internals/)

推荐答案

按照这个方法...

  • sw.js 文件放在 template 文件夹中
  • 配置视图作为静态文件

  • put the sw.js file in template folder
  • configure view to serve as static file

#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")

这篇关于在 Django 中使用 chrome 实现推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆