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')
Beispiel #2
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')