Example #1
0
    def test_send(self, mock_request):

        sin = pypn.Notification(pypn.OS)

        sin.send(the_id, payload)

        self.assertTrue(mock_request.called)
        self.assertIn('include_player_ids', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['include_player_ids'],
                         the_id)
        self.assertIn('heading', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['heading'],
                         payload['title'])
        self.assertIn('contents', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['contents'],
                         payload['body'])
        self.assertNotIn('data', mock_request.call_args[1])

        # Support data
        os_payload = payload.copy()
        data = {'a': 'dict'}
        os_payload['data'] = data

        sin.send(the_id, os_payload)

        self.assertIn('data', mock_request.call_args[1])
        self.assertEqual(data, mock_request.call_args[1]['data'])
Example #2
0
    def test_send_multiple_notification_payload(self, mock_request):
        sin = pypn.Notification(pypn.GCM)

        sin.send(ids, payload)

        self.assertTrue(mock_request.called)
        self.assertIn('registration_ids', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['registration_ids'], ids)
        self.assertIn('data', mock_request.call_args[1])
        self.assertIn('notification', mock_request.call_args[1])
Example #3
0
    def test_send_single_notification_payload(self, mock_request):
        sin = pypn.Notification(pypn.GCM)

        sin.send(the_id, payload)

        self.assertTrue(mock_request.called)
        self.assertIn('to', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['to'], the_id)
        self.assertIn('data', mock_request.call_args[1])
        self.assertIn('notification', mock_request.call_args[1])
Example #4
0
    def test_send_topic_notification_payload(self, mock_request):
        topic = 'topic'
        sin = pypn.Notification(pypn.GCM)

        sin.send(topic, payload)

        self.assertTrue(mock_request.called)
        self.assertIn('topic', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['topic'], topic)
        self.assertIn('data', mock_request.call_args[1])
        self.assertIn('notification', mock_request.call_args[1])
Example #5
0
    def test_send_single(self, mock_request):
        sin = pypn.Notification(pypn.APNS)

        sin.send(the_id, payload)

        self.assertTrue(mock_request.called)
        self.assertIn('device_token', mock_request.call_args[1])
        self.assertEqual(mock_request.call_args[1]['device_token'], the_id)
        self.assertIn('n', mock_request.call_args[1])
        self.assertIsInstance(mock_request.call_args[1]['n'],
                              pypn.apns2.Notification)
Example #6
0
    def test_send_multiple(self, mock_request):
        sin = pypn.Notification(pypn.APNS)

        sin.send(ids, payload)

        self.assertEqual(mock_request.call_count, 2)
        self.assertIn('device_token', mock_request.call_args_list[0][1])
        self.assertEqual(mock_request.call_args_list[0][1]['device_token'],
                         the_id)
        self.assertIn('n', mock_request.call_args_list[0][1])
        self.assertIsInstance(mock_request.call_args_list[0][1]['n'],
                              pypn.apns2.Notification)
        self.assertIn('device_token', mock_request.call_args_list[1][1])
        self.assertEqual(mock_request.call_args_list[1][1]['device_token'],
                         second_id)
        self.assertIn('n', mock_request.call_args_list[1][1])
        self.assertIsInstance(mock_request.call_args_list[1][1]['n'],
                              pypn.apns2.Notification)
Example #7
0
    def send(self):
        if self.canceled:
            return None
        # Avoid sending the notification again
        if self.sent_at is not None:
            return None
        notification = pypn.Notification(self.provider)
        result = notification.send(json.loads(self.tokens),
                                   json.loads(self.data))
        self.sent_at = datetime.datetime.now()
        # This should be handled by pypn. `result` can be `None`,
        # <str>, <requests.Response>(OneSignal) We only use OneSignal
        # so we will consider it's a `Response` that contains json.
        if result.status_code == requests.codes.ok:
            self.result = result.json()
        else:
            self.result = result.content
        self.save(update_fields=('sent_at', 'result'))

        return result
Example #8
0
data = {
    "body": "Hello world!",
    "title": "Yahoooo",
    "apns_launch_image": "default",
    "apns_badge": 1,
    "sound": 'x',
    "apns_content_available": 1,
    "apns_category": "My_custom_x",
    "apns_mutable_content": True,
    "priority": "high",
}

topic_data = data.copy()
data['data'] = {'Nick': 'Mario'}

gcmp = pypn.Notification(pypn.GCM)

result = gcmp.send(token, data)

result = gcmp.send(topic, topic_data)

result = gcmp.send([token_bad, token], data)

apns = pypn.Notification(pypn.APNS)

token = os.environ.get('TESTING_APNS_TOKEN')

response = apns.send(token, data)
print(response.status_code)
print(response.reason)
Example #9
0
    def test_send(self):
        sin = pypn.Notification(pypn.DUMMY)

        result = sin.send(ids, payload)

        self.assertEqual(result, (ids, payload))