Exemple #1
0
    def handle(self, *args, **options):
        if options['service'] is None:
            raise CommandError('The --service option is required')
        try:
            service_id = int(options['service'])
        except ValueError:
            raise CommandError(
                'The --service option should pass an id in integer format as its value'
            )
        if options['badge'] is not None:
            try:
                options['badge'] = int(options['badge'])
            except ValueError:
                raise CommandError(
                    'The --badge option should pass an integer as its value')
        try:
            service = APNService.objects.get(pk=service_id)
        except APNService.DoesNotExist:
            raise CommandError('APNService with id %d does not exist' %
                               service_id)

        message = options['message']
        extra = options['extra']

        if not message and not extra:
            raise CommandError(
                'To send a notification you must provide either the --message or --extra option.'
            )

        notification = Notification(message=options['message'],
                                    badge=options['badge'],
                                    service=service,
                                    sound=options['sound'])

        if options['persist'] is not None:
            notification.persist = options['persist']

        if extra is not None:
            notification.extra = json.loads(extra)

        try:
            chunk_size = int(options['chunk_size'])
        except ValueError:
            raise CommandError(
                'The --batch-size option should be an integer value.')

        if not notification.is_valid_length():
            raise CommandError(
                'Notification exceeds the maximum payload length. Try making your message shorter.'
            )

        service.push_notification_to_devices(notification,
                                             chunk_size=chunk_size)
        if 'test' not in sys.argv:
            self.stdout.write('Notification pushed successfully\n')
Exemple #2
0
def createNotification(message, notification_type, custom_payload=False, recipients=[]):
	service = APNService.objects.get(hostname='gateway.sandbox.push.apple.com', name='sandbox')
	notification = Notification(message=message, service=service)
	if custom_payload:
		notification.custom_payload = custom_payload
	notification.badge = None
	notification.notification_type = notification_type
	notification.save()
	for recipient in recipients:
		notification.recipients.add(recipient)
	return notification
Exemple #3
0
 def test_invalid_payload_size(self):
     n = Notification(message='.' * 260)
     self.assertRaises(NotificationPayloadSizeExceeded,
                       self.service.get_payload, n)