示例#1
0
    def send_notification(self, day=False, test=False):
        if not day:
            service = DayService(user=self.__user)
            day = service.get_current_date()
        morning_message, _ = self.get_or_create(day)

        if not self.__configuration.enabled:
            raise MorningMessageService.NotEnabled()

        serialized = MorningMessageSerializer(morning_message).data
        serialized['type'] = 'morning-message'

        if not serialized['text']:
            del serialized['text']
        if not serialized['anchor']:
            del serialized['anchor']

        if serialized['notification']:
            serialized['body'] = serialized['notification']
            del serialized['notification']

        push_service = PushMessageService(user=self.__user)
        message = push_service.send_notification(
            body=morning_message.notification,
            title='Morning check-in',
            data=serialized,
            collapse_subject='morning-message',
            send_message_id_only=True)
        morning_message.add_context(message)
        return message
示例#2
0
    def test_makes_message_receipt(self, send):
        user = self.make_user()
        push_message_service = PushMessageService(user)

        push_message_service.send_notification("Hello World")

        message_receipt = MessageReceipt.objects.get(message__recipient=user)
        self.assertEqual(message_receipt.type, MessageReceipt.SENT)
示例#3
0
    def send_message(self):
        push_message_service = PushMessageService(self.user)

        message_template = self.get_message_template()
        message = push_message_service.send_notification(
            message_template.body,
            title=message_template.title,
            data={'randomizationId': str(self.decision.id)},
            collapse_subject='activity-suggestion')
        DecisionContext.objects.create(decision=self.decision,
                                       content_object=message)
示例#4
0
    def test_sends_notification_with_collapse_subject(self, send):
        user = self.make_user()
        push_message_service = PushMessageService(user)

        push_message_service.send_notification(body='This is only a test',
                                               collapse_subject='test-subject')

        message = Message.objects.get(recipient=user)
        self.assertEqual(message.collapse_subject, 'test-subject')
        send_kwargs = send.call_args[1]
        self.assertEqual(send_kwargs['collapse_subject'], 'test-subject')
示例#5
0
    def test_sends_data(self, send):
        user = self.make_user()
        push_message_service = PushMessageService(user)

        result = push_message_service.send_data({
            'some': 'data',
            'example': 1234
        })

        self.assertTrue(result)
        message = Message.objects.get(recipient=user)
        send_kwargs = send.call_args[1]
        self.assertEqual(str(message.uuid), send_kwargs['data']['messageId'])
        self.assertEqual(message.message_type, Message.DATA)
示例#6
0
 def send_notification(self, activity_survey):
     serialized_survey = SurveySerializer(activity_survey)
     try:
         service = PushMessageService(user=self.configuration.user)
         message = service.send_notification(
             body='Tell us about the activity you just completed.',
             title='Activity Survey',
             collapse_subject='activity_survey',
             data={'survey': serialized_survey.data})
         return message
     except (PushMessageService.MessageSendError,
             PushMessageService.DeviceMissingError) as e:
         raise ActivitySurveyService.NotificationSendError(
             'Unable to send notification')
示例#7
0
 def send_notification(self):
     serialized_survey = SurveySerializer(self)
     try:
         service = PushMessageService(user=self.user)
         message = service.send_notification(
             body='Can you answer a couple of questions?',
             title='Walking Suggestion Survey',
             collapse_subject='walking_suggestion_survey',
             data={'survey': serialized_survey.data},
             send_message_id_only=True)
         return message
     except (PushMessageService.MessageSendError,
             PushMessageService.DeviceMissingError) as e:
         raise WalkingSuggestionSurvey.NotificationSendError(
             'Unable to send notification')
示例#8
0
    def test_sends_notification(self, send):
        user = self.make_user()
        push_message_service = PushMessageService(user)

        result = push_message_service.send_notification("Example message")

        self.assertTrue(result)
        message = Message.objects.get(recipient=user)
        send_kwargs = send.call_args[1]
        self.assertEqual(str(message.uuid), send_kwargs['data']['messageId'])
        self.assertEqual(message.message_type, Message.NOTIFICATION)
        self.assertEqual(message.body, "Example message")
        self.assertEqual(message.title, "HeartSteps")
        self.get_received_task.assert_called_with(
            countdown=300, kwargs={'message_id': message.id})
示例#9
0
    def test_no_user(self):
        user = User.objects.create(username="******")

        errored = False
        try:
            push_message_service = PushMessageService(user)
        except DeviceMissingError:
            errored = True

        self.assertTrue(errored)
示例#10
0
    def send_reflection(self, week, test=False):
        next_week = self.get_week_after(week)
        # Reset week goal, incase it was maniputlated
        # (which probably happened in testing)
        next_week.goal = next_week.get_default_goal()
        next_week.save()

        week_serialized = WeekSerializer(week)
        next_week_serialized = WeekSerializer(next_week)

        push_message_service = PushMessageService(user=self.__user)
        message = push_message_service.send_notification(
            body='Time for weekly reflection',
            title='Weekly reflection',
            data={
                'type': 'weekly-reflection',
                'currentWeek': week_serialized.data,
                'nextWeek': next_week_serialized.data
            },
            collapse_subject='weekly-reflection')
示例#11
0
    def request_context(self):
        try:
            push_message_service = PushMessageService(self.user)
        except PushMessageService.DeviceMissingError:
            raise DecisionService.Unreachable('No device')
        try:
            message = push_message_service.send_data({
                'type':
                'request-context',
                'decisionId':
                str(self.decision.id)
            })
        except PushMessageService.MessageSendError:
            raise DecisionService.Unreachable('Unable to send')

        if message:
            DecisionContext.objects.create(decision=self.decision,
                                           content_object=message)
            return True
        else:
            return False
示例#12
0
    def send_message(self,
                     parent_id: str,
                     message_id: str,
                     message_title: str,
                     message_body: str,
                     is_test: bool = False):
        push_message_service = PushMessageService(self.__user)

        if is_test:
            message_template = self.get_message_template(
                "test", "test", "Hello", "HeartSteps!")
        else:
            message_template = self.get_message_template(
                parent_id, message_id, message_title, message_body)

        self.sent_log = self.issue_sent_log(message_template)

        message = push_message_service.send_notification(
            message_template.message_body,
            title=message_template.message_title,
            data={'randomizationId': str(self.sent_log.id)},
            collapse_subject='generic-message')
        return message
示例#13
0
    def test_gets_most_recent_active_device(self):
        user = self.make_user()

        Device.objects.create(user=user,
                              token='newer-device-token',
                              type=Device.ANDROID,
                              active=True)
        Device.objects.create(user=user,
                              token='newer-deactivated-device-token',
                              type=Device.ANDROID,
                              active=False)

        push_message_service = PushMessageService(user)

        self.assertIsNotNone(push_message_service.device)
        self.assertEqual(push_message_service.device.token,
                         'newer-device-token')
示例#14
0
    def test_gets_user_device(self):
        user = self.make_user()

        push_message_service = PushMessageService(user)

        self.assertIsNotNone(push_message_service.device)
示例#15
0
    def test_handles_message_send_failure(self):
        user = self.make_user()
        push_message_service = PushMessageService(user)

        with self.assertRaises(push_message_service.MessageSendError):
            result = push_message_service.send_notification("Hello World")