forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [AXM-252] add settings for edx-ace push notifications (#2541)
* feat: [AXM-252] create policy for push notifications * feat: [AXM-252] add API for store device token * feat: [AXM-252] add settings for edx-ace push notifications * chore: [AXM-252] add edx-ace and django-push-notification to dev requirements * chore: [AXM-252] update edx-ace version * fix: [AXM-252] add create token edndpoint to urls * chore: [AXM-252] update django push notifications version * style: [AXM-252] fix code style issues after review * chore: [AXM-252] bump edx-ace version * refactor: [AXM-252] some push notif policy refactoring * chore: [AXM-252] change edx-ace branch to mob-develop * chore: [AXM-252] recompile requirements after rebase
- Loading branch information
1 parent
cdfa6fa
commit 97392f5
Showing
13 changed files
with
626 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
from .views import GCMDeviceViewSet | ||
|
||
|
||
CREATE_GCM_DEVICE = GCMDeviceViewSet.as_view({'post': 'create'}) | ||
|
||
|
||
urlpatterns = [ | ||
path('create-token/', CREATE_GCM_DEVICE, name='gcmdevice-list'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.conf import settings | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
|
||
from edx_ace.push_notifications.views import GCMDeviceViewSet as GCMDeviceViewSetBase | ||
|
||
from ..decorators import mobile_view | ||
|
||
|
||
@mobile_view(is_user=True) | ||
class GCMDeviceViewSet(GCMDeviceViewSetBase): | ||
""" | ||
**Use Case** | ||
This endpoint allows clients to register a device for push notifications. | ||
If the device is already registered, the existing registration will be updated. | ||
If setting PUSH_NOTIFICATIONS_SETTINGS is not configured, the endpoint will return a 501 error. | ||
**Example Request** | ||
POST /api/mobile/{version}/notifications/create-token/ | ||
**POST Parameters** | ||
The body of the POST request can include the following parameters. | ||
* name (optional) - A name of the device. | ||
* registration_id (required) - The device token of the device. | ||
* device_id (optional) - ANDROID_ID / TelephonyManager.getDeviceId() (always as hex) | ||
* active (optional) - Whether the device is active, default is True. | ||
If False, the device will not receive notifications. | ||
* cloud_message_type (required) - You should choose FCM or GCM. Currently, only FCM is supported. | ||
* application_id (optional) - Opaque application identity, should be filled in for multiple | ||
key/certificate access. | ||
**Example Response** | ||
```json | ||
{ | ||
"id": 1, | ||
"name": "My Device", | ||
"registration_id": "fj3j4", | ||
"device_id": 1234, | ||
"active": true, | ||
"date_created": "2024-04-18T07:39:37.132787Z", | ||
"cloud_message_type": "FCM", | ||
"application_id": "my_app_id" | ||
} | ||
``` | ||
""" | ||
|
||
def create(self, request, *args, **kwargs): | ||
if not getattr(settings, 'PUSH_NOTIFICATIONS_SETTINGS', None): | ||
return Response('Push notifications are not configured.', status.HTTP_501_NOT_IMPLEMENTED) | ||
|
||
return super().create(request, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Utility functions for edx-ace. | ||
""" | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def setup_firebase_app(firebase_credentials, app_name='fcm-app'): | ||
""" | ||
Returns a Firebase app instance if the Firebase credentials are provided. | ||
""" | ||
try: | ||
import firebase_admin # pylint: disable=import-outside-toplevel | ||
except ImportError: | ||
log.error('Could not import firebase_admin package.') | ||
return | ||
if firebase_credentials: | ||
certificate = firebase_admin.credentials.Certificate(firebase_credentials) | ||
return firebase_admin.initialize_app(certificate, name=app_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Policies for the notifications app.""" | ||
|
||
from edx_ace.channel import ChannelType | ||
from edx_ace.policy import Policy, PolicyResult | ||
from opaque_keys.edx.keys import CourseKey | ||
|
||
from .models import CourseNotificationPreference | ||
|
||
|
||
class CoursePushNotificationOptout(Policy): | ||
""" | ||
Course Push Notification optOut Policy. | ||
""" | ||
|
||
def check(self, message): | ||
""" | ||
Check if the user has opted out of push notifications for the given course. | ||
:param message: | ||
:return: | ||
""" | ||
course_ids = message.context.get('course_ids', []) | ||
app_label = message.context.get('app_label') | ||
|
||
if not (app_label or message.context.get('send_push_notification', False)): | ||
return PolicyResult(deny={ChannelType.PUSH}) | ||
|
||
course_keys = [CourseKey.from_string(course_id) for course_id in course_ids] | ||
for course_key in course_keys: | ||
course_notification_preference = CourseNotificationPreference.get_user_course_preference( | ||
message.recipient.lms_user_id, | ||
course_key | ||
) | ||
push_notification_preference = course_notification_preference.get_notification_type_config( | ||
app_label, | ||
notification_type='push', | ||
).get('push', False) | ||
|
||
if not push_notification_preference: | ||
return PolicyResult(deny={ChannelType.PUSH}) | ||
|
||
return PolicyResult(deny=frozenset()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.