Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send mobile braze notifications #36272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion openedx/core/djangoapps/notifications/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.translation import gettext_lazy as _

from .base_notification import COURSE_NOTIFICATION_APPS, COURSE_NOTIFICATION_TYPES
from .models import CourseNotificationPreference, Notification
from .models import CourseNotificationPreference, Notification, NotificationBrazeCampaigns


class NotificationAppNameListFilter(admin.SimpleListFilter):
Expand Down Expand Up @@ -108,5 +108,16 @@ def get_search_results(self, request, queryset, search_term):
return queryset, True


@admin.register(NotificationBrazeCampaigns)
class NotificationBrazeCampaignsAdmin(admin.ModelAdmin):
"""
Admin interface for the NotificationBrazeCampaigns model.
"""
list_display = ('notification_type', 'braze_campaign_id', 'created', 'modified')
readonly_fields = ['created', 'modified']
class Meta:
model = NotificationBrazeCampaigns


admin.site.register(Notification, NotificationAdmin)
admin.site.register(CourseNotificationPreference, CourseNotificationPreferenceAdmin)
19 changes: 19 additions & 0 deletions openedx/core/djangoapps/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,22 @@ def get_core_config(self, app_name) -> Dict:
}
"""
return self.get_notification_types(app_name).get('core', {})


class NotificationBrazeCampaigns(TimeStampedModel):
"""
Model to store campaign ids for notifications
"""
notification_type = models.CharField(max_length=64)
braze_campaign_id = models.CharField(max_length=128)

@classmethod
def get_notification_campaign_id(cls, notification_type):
try:
cls.objects.get(notification_type=notification_type).braze_campaign_id
except cls.DoesNotExist:
log.error("No campaign id found for notification type %s", notification_type)
return
except cls.MultipleObjectsReturned:
log.error("Multiple campaign ids found for notification type %s", notification_type)
return
31 changes: 31 additions & 0 deletions openedx/core/djangoapps/notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pytz import UTC

from common.djangoapps.student.models import CourseEnrollment
from lms.djangoapps.utils import get_braze_client
from openedx.core.djangoapps.notifications.audience_filters import NotificationFilter
from openedx.core.djangoapps.notifications.base_notification import (
get_default_values_of_preference,
Expand All @@ -28,6 +29,7 @@
from openedx.core.djangoapps.notifications.models import (
CourseNotificationPreference,
Notification,
NotificationBrazeCampaigns,
get_course_notification_preference_config_version
)
from openedx.core.djangoapps.notifications.utils import clean_arguments, get_list_in_batches
Expand Down Expand Up @@ -203,6 +205,35 @@ def send_notifications(user_ids, course_key: str, app_name, notification_type, c
generated_notification_audience, app_name, notification_type, course_key, content_url,
notification_content, sender_id=sender_id
)
send_braze_notification_to_mobile_users(generated_notification_audience, app_name, notification_type, course_key, content_url,notification_content, sender_id=sender_id)

def send_braze_notification_to_mobile_users(generated_notification_audience, app_name, notification_type, course_key, content_url,
notification_content, sender_id):
if app_name != 'discussion':
return

campaign_id = NotificationBrazeCampaigns.get_notification_campaign_id(notification_type)
if not campaign_id:
return

post_data = {
'trigger_properties': {
'notification_type': notification_type,
'course_id': course_key,
'topic_id': notification_content['extra_context']['topic_id'],
'thread_id': notification_content['extra_context']['thread_id'],
'content_url': content_url,
**notification_content,
},

}
emails = generated_notification_audience
try:
braze_client = get_braze_client()
if braze_client:
braze_client.send_campaign_message(campaign_id=campaign_id, trigger_properties=post_data, emails=emails)
except Exception as exc: # pylint: disable=broad-except
logger.error(f'Unable to send notification request with Braze ')


def is_notification_valid(notification_type, context):
Expand Down
Loading