Commit 4b6a1716 by Nimisha Asthagiri

Merge pull request #7805 from edx/mobile/push-notification-ios

MA-668 Mobile Push Notification payload update
parents 4a4c3c56 c8e0095f
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Helper methods for push notifications from Studio. Helper methods for push notifications from Studio.
""" """
from uuid import uuid4
from django.conf import settings from django.conf import settings
from logging import exception as log_exception from logging import exception as log_exception
...@@ -46,17 +47,36 @@ def send_push_course_update(course_key_string, course_subscription_id, course_di ...@@ -46,17 +47,36 @@ def send_push_course_update(course_key_string, course_subscription_id, course_di
settings.PARSE_KEYS["APPLICATION_ID"], settings.PARSE_KEYS["APPLICATION_ID"],
settings.PARSE_KEYS["REST_API_KEY"], settings.PARSE_KEYS["REST_API_KEY"],
) )
push_payload = {
"action": "course.announcement",
"notification-id": unicode(uuid4()),
"course-id": course_key_string,
"course-name": course_display_name,
}
push_channels = [course_subscription_id]
# Push to all Android devices
Push.alert(
data=push_payload,
channels={"$in": push_channels},
where={"deviceType": "android"},
)
# Push to all iOS devices
# With additional payload so that
# 1. The push is displayed automatically
# 2. The app gets it even in the background.
# See http://stackoverflow.com/questions/19239737/silent-push-notification-in-ios-7-does-not-work
push_payload.update({
"alert": "",
"content-available": 1
})
Push.alert( Push.alert(
data={ data=push_payload,
"course-id": course_key_string, channels={"$in": push_channels},
"action": "course.announcement", where={"deviceType": "ios"},
"action-loc-key": "VIEW_BUTTON",
"loc-key": "COURSE_ANNOUNCEMENT_NOTIFICATION_BODY",
"loc-args": [course_display_name],
"title-loc-key": "COURSE_ANNOUNCEMENT_NOTIFICATION_TITLE",
"title-loc-args": [],
},
channels=[course_subscription_id],
) )
except ParseError as error: except ParseError as error:
log_exception(error.message) log_exception(error.message)
...@@ -303,4 +303,14 @@ class CourseUpdateTest(CourseTestCase): ...@@ -303,4 +303,14 @@ class CourseUpdateTest(CourseTestCase):
def test_notifications_sent_to_parse(self, mock_parse_push): def test_notifications_sent_to_parse(self, mock_parse_push):
PushNotificationConfig(enabled=True).save() PushNotificationConfig(enabled=True).save()
self.post_course_update(send_push_notification=True) self.post_course_update(send_push_notification=True)
self.assertTrue(mock_parse_push.alert.called) self.assertEquals(mock_parse_push.alert.call_count, 2)
@override_settings(PARSE_KEYS={"APPLICATION_ID": "TEST_APPLICATION_ID", "REST_API_KEY": "TEST_REST_API_KEY"})
@patch("contentstore.push_notification.log_exception")
@patch("contentstore.push_notification.Push")
def test_notifications_error_from_parse(self, mock_parse_push, mock_log_exception):
PushNotificationConfig(enabled=True).save()
from parse_rest.core import ParseError
mock_parse_push.alert.side_effect = ParseError
self.post_course_update(send_push_notification=True)
self.assertTrue(mock_log_exception.called)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment