Skip to content

Commit

Permalink
[issue #5] Adding manifest.json functionality for chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
safwanrahman committed Jun 10, 2016
1 parent a5e0010 commit f99c621
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions webpush/templates/webpush.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

<script id="service-worker-js" type="text/javascript" src="{% static 'webpush/webpush_serviceworker.js' %}"></script>

<link rel="manifest" href="{% url 'webpush_manifest_json' %}">

<div id="webpush-data" {% if group %} data-group="{{ group }}" {% endif %} data-url="{{ url }}" hidden></div>
{% endif %}
1 change: 1 addition & 0 deletions webpush/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

urlpatterns = [
url(r'^save_information', views.save_info, name='save_webpush_info'),
url(r'^manifest', views.generate_manifest, name='webpush_manifest_json'),
]
11 changes: 8 additions & 3 deletions webpush/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# import requests
from .models import PushInformation, Group

from django.conf import settings
from django.forms.models import model_to_dict

from pywebpush import WebPusher
Expand All @@ -24,7 +24,12 @@ def send_notification_to_group(group_name, payload, ttl=0):
def _send_notification(push_info, payload, ttl):
subscription = push_info.subscription
subscription_data = _process_subscription_info(subscription)
req = WebPusher(subscription_data).send(data=payload, ttl=ttl)
# Check if GCM info is provided in the settings
if hasattr(settings,'WEBPUSH_SETTINGS'):
gcm_key = settings.WEBPUSH_SETTINGS.get('GCM_KEY')
else:
gcm_key = None
req = WebPusher(subscription_data).send(data=payload, ttl=ttl, gcm_key=gcm_key)
return req

def _process_subscription_info(subscription):
Expand All @@ -35,5 +40,5 @@ def _process_subscription_info(subscription):

return {
"endpoint": endpoint,
"keys": {"p256dh": str(p256dh), "auth": str(auth)}
"keys": {"p256dh": p256dh, "auth": auth}
}
15 changes: 12 additions & 3 deletions webpush/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import json

from django.conf import settings
from django.core.exceptions import FieldError
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.views.decorators.http import require_POST, require_GET

from .config import MANIFEST
from .forms import WebPushForm, SubscriptionForm



@require_POST
@csrf_exempt
def save_info(request):
Expand Down Expand Up @@ -51,6 +52,14 @@ def save_info(request):
return HttpResponse(status=400)


@require_GET
def generate_manifest(request):
if hasattr(settings,'WEBPUSH_SETTINGS'):
return JsonResponse(MANIFEST)
else:
return HttpResponse(status=404)


def process_subscription_data(post_data):
"""Process the subscription data according to out model"""
subscription_data = post_data.pop("subscription", {})
Expand Down

0 comments on commit f99c621

Please sign in to comment.