.. module:: notifications.providers
Django notifs comes with a set of inbuilt providers. These providers are typically classes that accept a payload and contain the logic for delivering the payload to an external service.
Below are the list of supported providers:
.. autoclass:: EmailNotificationProvider
name: 'email'
The email provider uses the standard django.core.mail
module.
This opens up support for multiple ESP's (Mailjet, Mailchimp, sendgrid etc)
Optional dependency for django-anymail:
pip install django-notifs[anymail]
If you use django-anymail
or a custom Email backend, all you have to do configure the settings and dependencies as you'd
normally do and the email provider should pick it up.
.. autopydantic_model:: notifications.providers.email.EmailSchema
You can still pass extra keyword arguments like tags
(depending on the ESP that you use.)
See the django-anymail
documentation for more information.
.. autoclass:: DjangoSMSNotificationProvider
name: 'django_sms'
The SMS provider uses a third-party app called django-sms this also opens up support for multiple SMS providers.
Supported providers are:
- Twilio
- Message bird
pip install django-notifs[django_sms]
Extra dependencies can be installed by:
pip install django-sms[twilio,messagebird]
See the django-sms documentation for more information on how to configure your preferred backend. Once it is configured,
django-notifs
should pick it up
.. autopydantic_model:: notifications.providers.django_sms.DjangoSmsSchema
.. autoclass:: SlackNotificationProvider
name: 'slack'
pip install django-notifs[slack]
NOTIFICATIONS_SLACK_BOT_TOKEN=xxxxxxx
.. autopydantic_model:: notifications.providers.slack.SlackSchema
.. autoclass:: PusherChannelsNotificationProvider
name: 'pusher_channels'
pip install django-notifs[pusher_channels]
NOTIFICATIONS_PUSHER_CHANNELS_URL=https://<app_id>:<app_secret>@api-eu.pusher.com/apps/0000000
.. autopydantic_model:: notifications.providers.pusher_channels.PusherChannelsSchema
.. autoclass:: FCMNotificationProvider
name: 'fcm'
NOTIFICATIONS_FCM_API_KEY=xxxxxxx # FCM Api key NOTIFICATIONS_FCM_PROXY = {} # FCM proxy
Single:
.. autopydantic_model:: notifications.providers.fcm.FCMWebSchema
Bulk:
.. autopydantic_model:: notifications.providers.fcm.BulkFCMWebSchema
.. autoclass:: FCMWebNotificationProvider
name: 'fcm_web'
NOTIFICATIONS_FCM_KEY=xxxxxxx
Single:
{ 'title': 'notification title', 'body': 'body', 'click_action': 'https://example.com', 'icon': 'icon, 'to': 'user_token', }
django-notifs uses tweepy to deliver twitter notifiations
.. autoclass:: TwitterStatusUpdateNotificationProvider
name: 'twitter_status_update'
pip install django-notifs[twitter]
Twitter consumer key
Twitter consumer secret
Twitter access token
Twitter access token secret
.. autopydantic_model:: notifications.providers.twitter_status_update.TwitterStatusUpdateSchema
See the tweepy documentation for more information on these parameters
.. autoclass:: DjangoChannelsNotificationProvider
name: 'django_channels'
pip install django-notifs[channels]
Default='notifs_websocket_message'
The type
value of the messages that are going to received by the django notifs websocket consumer.
In most cases, you don't need to change this setting.
Default = 'room_name'
The WebSocket URL param name. It's also used to construct the WebSocket URL. See the :ref:`Advanced usage <Notification channels>` section for more information.
{ 'channel_layer': "Custom django channels layer or 'default'", 'destination': 'Group/channel name' }
.. autopydantic_model:: notifications.providers.django_channels.DjangoChannelsSchema
Sometimes, the inbuilt providers are not sufficient to handle every use case.
You can create a custom provider by inheriting from the Base provider class or an existing Provider and Implementing the
get_validator
/validate
, send
and send_bulk
method.
The Notification context is also available as a property (self.context
):
from typing import Dict, List from pydantic import BaseModel from notifications.providers import BaseNotificationProvider class CustomProviderSchema(BaseModel): event: str message: Dict class BulkCustomProviderSchema(BaseModel): group: str messages: List[CustomProviderSchema] class CustomNotificationProvider(BaseNotificationProvider): name = 'custom_provider' validator = CustomProviderSchema def get_validator(self): """Return a custom validator based on the context.""" if self.context.get('bulk', False) is True: return BulkCustomProviderSchema return CustomProviderSchema def validate(self, payload): """Validate without pydantic.""" pass def send(self, payload): # call an external API? pass def send_bulk(self, payloads): for payload in payloads: self.send(payload) # or call an external bulk API?