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

查看:388
本文介绍了在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/


一个微妙的注册方法是服务的位置
工作文件。在这种情况下您会注意到,服务工作者文件
位于域的根目录。这意味着服务工作者的
范围将是整个来源。换句话说,这个服务工作者
会收到这个域上的所有内容的提取事件。如果我们
在/example/sw.js注册服务工作者文件,那么服务
worker只会看到页面的抓取事件,其URL以
/ example /(ie / 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文件放在应用程序的根目录下?目前,服务人员的范围是: 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 文件放在模板文件夹中

  • 将视图配置为静态文件

  • 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天全站免登陆