{ "info": { "author": "Safwan Rahman", "author_email": "safwan.rahman15@gmail.com", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Framework :: Django", "Framework :: Django :: 1.8", "Intended Audience :: Developers", "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: Dynamic Content" ], "description": "\ufeff\nDjango-Webpush\n===================\n[](https://saythanks.io/to/safwanrahman)\n\nDjango-Webpush is a Package made for integrating and sending [Web Push Notification](https://developer.mozilla.org/en/docs/Web/API/Push_API) in Django Application.\n\nCurrently, it Supports Sending Push Notification to **Firefox 46+ and Chrome 52+**.\n\n----------\n\n\nInstallation and Setup\n-------------\n\nYou can install it easily from pypi by running\n\n pip install django-webpush\n\nAfter installing the package, add `webpush` in in your `INSTALLED_APPS` settings\n\n```python\nINSTALLED_APPS = (\n ...\n 'webpush',\n)\n```\n\nIf you would like to send notification to Google Chrome Users, you need to add a ``WEBPUSH_SETTINGS`` entry with the **Vapid Credentials** Like following:\n```python\nWEBPUSH_SETTINGS = {\n \"VAPID_PUBLIC_KEY\": \"Vapid Public Key\",\n \"VAPID_PRIVATE_KEY\":\"Vapid Private Key\",\n \"VAPID_ADMIN_EMAIL\": \"admin@example.com\"\n}\n```\n**Replace ``\"Vapid Public Key\"`` and ``\"Vapid Private Key\"`` with your Vapid Keys. Also replace ``admin@example.com`` with your email so that the push server of browser can reach to you if anything goes wrong.**\n\n> **To know how to obtain Vapid Keys please see this [`py_vapid`](https://github.com/web-push-libs/vapid/tree/master/python) and [Google Developer Documentation](https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user#how_to_create_application_server_keys). You can obtain one easily from [web-push-codelab.glitch.me](https://web-push-codelab.glitch.me/). ``Application Server Keys`` and ``Vapid Keys`` both are same.**\n\nThen include `webpush` in the `urls.py`\n\n```python\nurlpatterns = [\n url(r'^webpush/', include('webpush.urls'))\n]\n ```\n\n\n`django-webpush` is shipped with built in **`jinja`** support.\nIf you would like to use with jinja backend,\npass ``pipeline.jinja2.PipelineExtension`` to your jinja environment. Like following:\n\n```python\n{\n \"BACKEND\": \"django_jinja.backend.Jinja2\",\n \"OPTIONS\": {\n 'extensions': ['webpush.jinja2.WebPushExtension'],\n }\n},\n```\n\n\n**Then run Migration by ***`python manage.py migrate`*****\n\n\n\nAdding Web Push Information in Template\n-------------------\n\nSo in template, you need to load `webpush_notifications` custom template tag by following:\n- If you are using built in templating engine, add `{% load webpush_notifications %}` in the template\n- If you are using **jinja** templating engine, you do not need to load anything.\n\nNext, inside the `
` tag add `webpush_header` according to your templating engine:\n\n```html\n\n # For django templating engine\n {% webpush_header %}\n # For jinja templating engine\n {{ webpush_header() }}\n\n```\nNext, inside the `` tag, insert `webush_button` where you would like to see the **Subscribe to Push Messaging** Button. Like following\n\n```html\n\nHello World!
\n # For django templating engine\n {% webpush_button %}\n # For jinja templating engine\n {{ webpush_button() }}\n\n```\n\n >**Note:** The Push Notification Button will show only if the user is logged in or any `group` named is passed through `webpush` context\n\n ***If you would like to mark the subscription as a group, like all person subscribe for push notification from the template should be marked as group and would get same notification, you should pass a `webpush` context to the template through views. The `webpush` context should have a dictionary like `{\"group\": group_name}`*** . Like following\n\n```python\n webpush = {\"group\": group_name } # The group_name should be the name you would define.\n\nreturn render(request, 'template.html', {\"webpush\":webpush})\n```\n> **Note:** If you dont pass `group` through the `webpush` context, only logged in users can see the button for subscription and able to get notification.\n\n----------\n\nSending Web Push Notification\n-------------------\n\nA Web Push generally have a header and body. According to the W3C Specification, the data should be encrypted in transmission. The data is addressed as payload generally. Also a TTL header should be included indicating how much time the web push server store the data if the user is not online.\nSo in order to send notification, see below.\n\n- If you would like to send notification to a specific group, do like following:\n\n\n ```python\n from webpush import send_group_notification\n\n payload = {\"head\": \"Welcome!\", \"body\": \"Hello World\"}\n\n send_group_notification(group_name=\"my_group\", payload=payload, ttl=1000)\n # All subscribe subscribe through \"my_group\" will get a web push notification.\n # A ttl of 1000 is passed so the web push server will store\n # the data maximum 1000 seconds if any user is not online\n\n ```\n\n- If you would like to send Notification to a specific user, do like following\n ```python\n from webpush import send_user_notification\n\n payload = {\"head\": \"Welcome!\", \"body\": \"Hello World\"}\n\n send_user_notification(user=user, payload=payload, ttl=1000)\n # Here in the user parameter, a user object should be passed\n # The user will get notification to all of his subscribed browser. A user can subscribe many browsers.\n ```\n\n **And the subscribers will get a notification like:**\n\n\n\n- If you notification should have an icon or open a url when clicked, you can add those to the payload:\n\n ``` python\n from webpush import send_user_notification\n\n from webpush import send_group_notification\n\n payload = {\"head\": \"Welcome!\", \"body\u201d: \"Hello World\", \n \"icon\": \"https://i.imgur.com/dRDxiCQ.png\u201c, \"url\": \"https://www.example.com\"}\n\n send_group_notification(group_name=\"my_group\", payload=payload, ttl=1000)\n ```\n**And the subscribers will get a notification like:**\n\n\n\n**That will open https://www.example.com if clicked.**\n\n- If you want fine grained control over sending a single push message, do like following\n\n\n ```python\n from webpush.utils import send_to_subscription\n\n payload = {\"head\": \"Welcome!\", \"body\": \"Hello World\"}\n\n user = request.user\n push_infos = user.webpush_info.select_related(\"subscription\")\n for push_info in push_infos:\n send_to_subscription(push_info.subscription, payload)\n\n ```\n\n\n\n\n\n **And the subscribers will get a notification like**\n \n\n\nLicense\n=======\n----\nCopyright \u00a9 2018 Safwan Rahman\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU General Public License for more details.\n\n You should have received a copy of the GNU General Public License\n along with this program. If not, see