Guide to setup
djnotifier
into your Django project
- System requirements:
Redis server
pip3
packages -and off coursechannels channels-redis
django
itself.
pip install djnotifier
Add djnotifier to your INSTALLED_APPS
setting like this
INSTALLED_APPS = [
...
'djnotifier',
]
Setup asgi
application for the project
- Update
asgi.py
# <project>/asgi.py import os from django.core.asgi import get_asgi_application from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter # import web-socket routes from djnotifier from djnotifier.routing import websocket_urlpatterns os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project_name>.settings') application = ProtocolTypeRouter({ "http": get_asgi_application(), "websocket": AuthMiddlewareStack( URLRouter( websocket_urlpatterns ) ), })
- Configure
asgi
application insettings
# <project>/config.py ... ASGI_APPLICATION = '<project>.asgi.application'
Configure redis
layer
# <project>/config.py
...
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
...
Add djnotifier
's template
to a common project template e.g. base.html or core.html
<!--templates/core.html-->
{% include 'djnotifier/dj_notifier.html' %}
Publish notification
# send notification to an user
from djnotifier.utils import push_notification_to_user
data = {
"type": "success",
"message": "Congratulations! Your account is now verified"
}
push_notification_to_user(data, request.user)
# send notification to un-authenticated user(s)
from djnotifier.utils import push_notification_to_anonymous_users
data = {
"type": "info",
"message": "Welcome to our page!"
}
push_notification_to_anonymous_users(data)
Show/consume push notification from frontend
DJNotifier(style='info', text="Hi, welcome to our website!", audio=true)
Check usage page for more information.