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) notification = Notification(message=options['message'], badge=options['badge'], service=service, sound=options['sound']) if options['persist'] is not None: notification.persist = options['persist'] extra = options['extra'] if extra is not None: notification.extra = json.loads(extra) 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) if 'test' not in sys.argv: self.stdout.write('Notification pushed successfully\n')
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')
def handle(self, *args, **options): if options['message'] is None: raise CommandError('The --message option is required') 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) if not Notification.is_valid_length( options['message'], options['badge'], options['sound']): raise CommandError( 'Notification exceeds the maximum payload length. Try making your message shorter.' ) notification = Notification.objects.create(message=options['message'], badge=options['badge'], service=service, sound=options['sound']) service.push_notification_to_devices(notification) self.stdout.write('Notification pushed successfully\n')
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
def test_invalid_payload_size(self): n = Notification(message='.' * 260) self.assertRaises(NotificationPayloadSizeExceeded, self.service.get_payload, n)
def test_valid_length(self): self.assertTrue(Notification.is_valid_length( self.notification.message))
def test_invalid_length(self): long_message = '.' * 260 self.assertFalse(Notification.is_valid_length(long_message))
def test_valid_length(self): self.assertTrue(Notification.is_valid_length(self.notification.message))