Package: baseapp-notifications
BaseApp NotificationsReusable app to have in-app notifications integrated with email and push notifications. Its built on top ofdjango-notifications-hq.Based on theActivity Streams Spec.Notifications are actually actions events, which are categorized by four main components.Actor. The object that performed the activity.Verb. The verb phrase that identifies the action of the activity.Action Object.(Optional)The object linked to the action
itself.Target.(Optional)The object to which the activity was
performed.Actor,Action ObjectandTargetareGenericForeignKeysto any
arbitrary Django object. An action is a description of an action that
was performed (Verb) at some instant in time by someActoron some
optionalTargetthat results in anAction Objectgetting
created/updated/deleted.For example:nossila(actor)opened(verb)pull request
18(action_object)onbaseapp-backend(target)12 hours agoWhats missingFinish implementing push notificationsDRF views and serializersHow to installRequirements:baseapp-core>= 0.2.0django-notifications-hq>= 1.8**django-push-notifications >= 3.0.2Runpip install baseapp-notificationsAnd make sure to add the frozen version to yourrequirements/base.txtfileIf you want to develop,install using this other guide.Setup In-App notifications1 - Add to yourINSTALLED_APPS:INSTALLED_APPS=["baseapp_notifications",]2 - Set the notification model in yoursettings/base.py:NOTIFICATIONS_NOTIFICATION_MODEL="baseapp_notifications.Notification"Check how to customize to your own modelbellow.3 - Make sure to add the task routing forsend_push_notificationCELERY_TASK_ROUTES={"baseapp_notifications.tasks.send_push_notification":{"exchange":"default","routing_key":"default",},}4 - Make sure that your mainUser'sDjangoObjectTypeimplements interfaceNotificationsInterface:frombaseapp_core.graphqlimportDjangoObjectTypefrombaseapp_notifications.graphql.object_typesimportNotificationsInterfaceclassUserNode(DjangoObjectType):classMeta:interfaces=(relay.Node,NotificationsInterface)5 - Then you can expose notification's mutations and subscriptions:importgraphenefrombaseapp_notifications.graphql.mutationsimportNotificationsMutationsfrombaseapp_notifications.graphql.subscriptionsimportNotificationsSubscriptionclassQuery(graphene.ObjectType):me=graphene.Field(UserNode)defresolve_me(self,info):ifinfo.context.user.is_authenticated:returninfo.context.userclassMutation(graphene.ObjectType,NotificationsMutations):passclassSubscription(graphene.ObjectType,NotificationsSubscription):passschema=graphene.Schema(query=Query,mutation=Mutation,subscription=Subscription)Setup Push notifications (apple and google)1 - Add to yourINSTALLED_APPS:INSTALLED_APPS=["push_notifications"]2 - Set the push notifications settings in yoursettings/base.py:PUSH_NOTIFICATIONS_SETTINGS={"FCM_API_KEY":"[your api key]","APNS_CERTIFICATE":["Absolute path to your APNS certificate file"],"APNS_TOPIC":["Apple app ID"],"DEFAULT_CLOUD_MESSAGE_TYPE":"FCM","UPDATE_ON_DUPLICATE_REG_ID":True/False,"APNS_USE_SANDBOX":True/False,}You can get more details about this settings dict at thedjango-push-notifications oficial repository.3 - Set the push notification routes in yourrouter.pyfrompush_notifications.api.rest_frameworkimport(# noqaAPNSDeviceAuthorizedViewSet,GCMDeviceAuthorizedViewSet,WNSDeviceAuthorizedViewSet,WebPushDeviceAuthorizedViewSet,)router.register(r"push-notifications/apns",APNSDeviceAuthorizedViewSet,basename="apns")router.register(r"push-notifications/gcm",GCMDeviceAuthorizedViewSet,basename="gcm")router.register(r"push-notifications/wns",WNSDeviceAuthorizedViewSet,basename="wns")router.register(r"push-notifications/web",WebPushDeviceAuthorizedViewSet,basename="web")How to send a notificationfrombaseapp_notificationsimportsend_notificationsend_notification(add_to_history=True,send_push=True,send_email=True,sender=user,recipient=user,verb="opened",action_object=pr,target=repository,level="info",description=_("{user_name}opened a pull request in{repository_name}").format(user_name=user.name,repository_name=repository.name),push_title=_("title"),push_description=_("description"),extra={},)Arguments:add_to_history: A boolean (default=True). True to add this to your in-app notifications history.send_push: A boolean (default=True). True to send push notification to user's devices.send_email: A boolean (default=True). True to send notification via email.sender: An object of any type. (Required)recipient: AGroupor aUser QuerySetor a list ofUser. (Required)verb: An string. (Required)action_object: An object of any type. (Optional)target: An object of any type. (Optional)level: One of Notification.LEVELS ('success', 'info', 'warning', 'error') (default=info). (Optional)description: An string. (Optional)notification_url: URL used in emails so users can open this notification's page. (Optional)email_subject: An string (default=description). This will override the email's subject message. (Optional)email_message: An string (default=description). This will override the email's body message. (Optional)public: An boolean (default=True). (Optional)timestamp: An tzinfo (default=timezone.now()). (Optional)push_description: A string. (Required). The "body" of your push notificationpush_title: A string. (Optional). This will override the Title of your notification (apple and google uses the name defined on your build files)extra: A dict with data of any type. (Optional)Extra data: ou can also send any arbitrary kwargs and they will be added toNotification.dataJSONField.Email notificationsTo send email notifications make sure to setsend_email=Trueargument andnotification_urlso users can open the notification in the browser. Thedescriptionwill be used both as email's subject and email's body by default, check how to customize bellow.send_notification(sender=user,recipient=user,verb="opened",description="email's subject",add_to_history=True,send_email=True,notification_url="https://github.com/silverlogic/baseapp-backend/pull/18",)How to customize email notification templatesBy default it will look for templates based in yourslugify(verb), so can implement the following files with the following content in your project for each verb you want to customize:Createtemplates/emails/notifications/{verb_slugified}-subject.txt.j2with:{% include "emails/notification-subject.txt.j2" %}Createtemplates/emails/notifications/{verb_slugified}-body.txt.j2with:{% extends "emails/notification-body.txt.j2" %}Createtemplates/emails/notifications/{verb_slugified}-body.html.j2with:{% extends "emails/notification-body.html.j2" %}You can copy the extended templates over to your project to customize them as well.Also checktheir source code in this repositoryto understand how you can customize the notification message for each notification's verb, for example, to customize the email notification message (defaults to what you passed todescription):Example{# templates/emails/notifications/opened-body.html.j2 #}
{% extends "emails/notification-body.html.j2" %}

{% block notification_message %}
    {{ recipient.name }}'s custom notification message.
{% endblock %}Public API documentationIn you GraphQL schema it will expose the following queries, mutations and subscriptions. Please check your GraphiQL playground for better understandingQueriesquery{me{notificationsUnreadCountnotifications}}Mutationsmutation{notificationsMarkAllAsRead(input:{read:true}){recipient{notificationsUnreadCount}}notificationsMarkAsRead(input:{notificationIds:["Tm90aWZpY2F0aW9uOjE=","Tm90aWZpY2F0aW9uOjI="],read:true}){recipient{notificationsUnreadCount}}}Subscriptionssubscription{onNotificationChange{createdNotification@prependEdge(connections:$connections){node{...NotificationCardFragmentrecipient{idnotificationsUnreadCount}}}updatedNotification{...NotificationCardFragment}deletedNotificationId@deleteRecord}}How to customize Notification's ModelCreate yourcustom_notificationsdjango app inside your project and inherit fromAbstractNotificationlike:frombaseapp_notifications.baseimportAbstractNotificationclassNotification(AbstractNotification):my_custom_field=models.CharField()classMeta(AbstractNotification.Meta):abstract=FalseThen make sure to change yoursettings/base.pylike:INSTALLED_APPS=["baseapp_notifications","custom_notifications",# need to have both]NOTIFICATIONS_NOTIFICATION_MODEL="custom_notifications.Notification"That's it,my_custom_fieldshould be available in your GraphQL's Notification ObjectType as well.How to developGeneral development instructions can be found inmain README.