Integrating notification support into your app is a simple two-step process.
- create your notice types
- send notifications
Creating Notice Types
You need to call create_notice_type(label, display, description)
once to create the notice types for your application in the database. label
is just the internal shortname that will be used for the type, display
is what the user will see as the name of the notification type and description
is a short description.
For example:
notification.create_notice_type("friends_invite", "Invitation Received", "you have received an invitation")
One good way to automatically do this notice type creation is in a management.py
file for your app, attached to the syncdb signal. Here is an example:
``` from django.conf import settings from django.db.models import signals from django.utils.translation import ugettext_noop as _
if "notification" in settings.INSTALLED_APPS: from notification import models as notification
def create_notice_types(app, created_models, verbosity, **kwargs):
notification.create_notice_type("friends_invite", _("Invitation Received"), _("you have received an invitation"))
notification.create_notice_type("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))
signals.post_syncdb.connect(create_notice_types, sender=notification)
else: print "Skipping creation of NoticeTypes as notification app not found" ```
Notice that the code is wrapped in a try clause so if django-notification is not installed, your app will proceed anyway.
Note that the display and description arguments are marked for translation by using ugettext_noop. That will enable you to use Django's makemessages management command and use django-notification's i18n capabilities.
Sending Notification
To send a message you use send(users, notice_type_label, message_template, object_list, issue_notice)
where object_list
and issue_notice
are optional.
users
is a list of the users who should receive the notice. notice_type_label
is the label you used in the previous step to identify the notice type. message_template
is just a string, however if object_list
is non-empty, then message_template
should contain as many %s
as there are objects in object_list
. These will be replaced by references to the corresponding objects in object_list
.
For example:
notification.send([to_user], "friends_invite", "%s has requested to add you as a friend.", [from_user])
will sent a friend_invite
notice to to_user
with the message XXX has requested to add you as a friend.
where XXX is a reference to the from_user
object. Depending on the notification medium, this reference may be a link or just plain text.
issue_notice
is True
by default but you can set it to False
if you want a notification sent but not persisted as a Notice
object in the database.
In case you want to use django-notification in your reusable app, you can wrap the import of django-notification in a conditional clause that tests if it's installed before sending a notice. As a result your app or project still functions without notification.
For example:
``` from django.conf import settings
if "notification" in settings.INSTALLED_APPS: from notification import models as notification else: notification = None ```
and then, later:
if notification:
notification.send([to_user], "friends_invite", "%s has requested to add you as a friend.", [from_user])