Implementing push notification using chrome in Django

后端 未结 2 1377
粉色の甜心
粉色の甜心 2020-12-17 06:21

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

相关标签:
2条回答
  • 2020-12-17 07:02

    Similar to the accepted answer, but shorter:

    • Place service_worker.js in root of the template folder.
    • Add to your routings:

      from django.conf.urls import url
      from django.views.generic import TemplateView
      
      urlpatterns = [
          # Other urls
          url(r'^service_worker(.*.js)$',
              TemplateView.as_view(template_name='service_worker.js', 
                  content_type='application/x-javascript'))
          ]
      

    Update: I ended up needing to pass authentication credentials to the service_worker.js file, so this was my final route:

    url(r'^service_worker(.*.js)(?:/(?P<params>[a-zA-Z]+)/)?', 
            TemplateView.as_view(template_name='service_worker.js', content_type='application/x-javascript'))
    

    This allows passing parameters like so: domainbase.com/service_worker.js?foo=bar...

    The javascript to then access the params is:

    var url_params = location.search.substring(1);
    console.log(url_params);
     => "foo=bar..."
    
    0 讨论(0)
  • 2020-12-17 07:03

    Follow this method...

    • 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")
      
    0 讨论(0)
提交回复
热议问题