Skip to content

Latest commit

 

History

History
125 lines (103 loc) · 2.68 KB

QUICK_START.md

File metadata and controls

125 lines (103 loc) · 2.68 KB

Quick start guide

Guide to setup djnotifier into your Django project

Table of contents

Requirements

  1. System requirements: Redis server
  2. pip3 packages -
    channels
    channels-redis
    
    and off course django itself.

Install and configure

Step-0

pip install djnotifier

Step-1

Add djnotifier to your INSTALLED_APPS setting like this

INSTALLED_APPS = [
    ...
    'djnotifier',
]

Step-2

Setup asgi application for the project

  1. 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
            )
        ),
    })
  2. Configure asgi application in settings
    # <project>/config.py
    ...
    ASGI_APPLICATION = '<project>.asgi.application'

Step-3

Configure redis layer

# <project>/config.py
...

CHANNEL_LAYERS = {
   'default': {
      'BACKEND': 'channels_redis.core.RedisChannelLayer',
      'CONFIG': {
       "hosts": [('127.0.0.1', 6379)],
      },
   },
}

...

Step-4

Add djnotifier's template to a common project template e.g. base.html or core.html

<!--templates/core.html-->
{% include 'djnotifier/dj_notifier.html' %}

Usage

Backend

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)

Frontend

Show/consume push notification from frontend

DJNotifier(style='info', text="Hi, welcome to our website!", audio=true)

Demo

Check usage page for more information.

UI Demo