def _send_apns(registration_ids, message, **kwargs):
    '''
    Send a message to one or more APNS recipients

    :param registration_ids: a single or iterable collection of registration ids (APNS tokens)
    :param message: the payload to send. This is sent as the value of the 'alert' APNS key
    :param kwargs: additional APNS arguments. See push_notifications.apns._apns_sendd
    '''

    enqueue_date_str = kwargs.pop('enqueue_date', None)

    try:
        if isinstance(registration_ids, collections.Iterable):
            apns_send_bulk_message(registration_ids, message, **kwargs)
        else:
            apns_send_message(registration_ids, message, **kwargs)
        log_message_sent(enqueue_date_str=enqueue_date_str)
    except Exception as exception:
        logger.exception("Exception sending APNS message. %s : %s" %
                         (exception.__class__.__name__, str(exception)))

        # We log a 'message sent with exception' event as well as the full exception itself
        log_message_sent(exception=exception,
                         enqueue_date_str=enqueue_date_str)
        analytics.exception()
    def handle(self, *args, **options):
        import pytz
        from datetime import datetime
        announcements = AnnouncementModel.objects.all().filter(sent=False, approved=True, broadcast_at__lte=datetime.now(pytz.utc))
        for announcement in announcements:
            announcement.sent = True
            announcement.save()  # Save immediately so even if this takes time to run, we won't have duplicate pushes
            if announcement.category and (announcement.category & 1 == 0):
                apns_devices = APNSDevice.objects.all().filter(active=True).extra(where=['CAST(name as INTEGER) & %s != 0'],
                                                                                  params=str(announcement.category))
                gcm_devices = GCMDevice.objects.all().filter(active=True).extra(where=['CAST(name as INTEGER) & %s != 0'],
                                                                                params=str(announcement.category))
            else:
                apns_devices = APNSDevice.objects.all().filter(active=True)
                gcm_devices = GCMDevice.objects.all().filter(active=True)

            for i in range(0, len(apns_devices), 50):
                import time
                try:
                    aps_data = {"alert": {"title": announcement.title, "body": announcement.info},
                                "sound": "default"}
                    reg_ids = map(lambda d: d.registration_id, apns_devices[i:i + 50])
                    apns_send_bulk_message(registration_ids=reg_ids, alert=None,
                                           extra={"aps": aps_data, "category": announcement.category, "title": announcement.title})
                    time.sleep(1)
                except APNSDataOverflow:
                    pass
            gcm_devices.send_message(announcement.info)
def _send_apns(registration_ids, message, **kwargs):
    '''
    Send a message to one or more APNS recipients

    :param registration_ids: a single or iterable collection of registration ids (APNS tokens)
    :param message: the payload to send. This is sent as the value of the 'alert' APNS key
    :param kwargs: additional APNS arguments. See push_notifications.apns._apns_sendd
    '''

    # Strip whitespace from APNS Registration Ids. This is also done on ingestion for new registration_ids
    registration_ids = [reg_id.replace(" ", "") for reg_id in registration_ids]

    enqueue_date_str = kwargs.pop('enqueue_date', None)

    try:
        if isinstance(registration_ids, collections.Iterable):
            apns_send_bulk_message(registration_ids, message, **kwargs)
        else:
            apns_send_message(registration_ids, message, **kwargs)
        log_message_sent(enqueue_date_str=enqueue_date_str)
    except Exception as exception:
        logger.exception("Exception sending APNS message. %s : %s" % (exception.__class__.__name__, str(exception)))

        # We log a 'message sent with exception' event as well as the full exception itself
        log_message_sent(exception=exception, enqueue_date_str=enqueue_date_str)
        analytics.exception()
Example #4
0
def _send_apns(registration_ids, message, **kwargs):
    '''
    Send a message to one or more APNS recipients

    :param registration_ids: a single or iterable collection of registration ids (APNS tokens)
    :param message: the payload to send. This is sent as the value of the 'alert' APNS key
    :param kwargs: additional APNS arguments. See push_notifications.apns._apns_sendd
    '''

    # Strip whitespace from APNS Registration Ids. This is also done on ingestion for new registration_ids
    registration_ids = [reg_id.replace(" ", "") for reg_id in registration_ids]

    enqueue_date_str = kwargs.pop('enqueue_date', None)

    priority = 'low'
    if message.get('body', None) is not None:
        priority = 'high'

    try:
        if isinstance(registration_ids, collections.Iterable):
            apns_send_bulk_message(registration_ids, message, **kwargs)
        else:
            apns_send_message(registration_ids, message, **kwargs)
        log_message_sent(enqueue_date_str=enqueue_date_str, priority=priority)
    except Exception as exception:
        logger.exception("Exception sending APNS message. %s : %s" %
                         (exception.__class__.__name__, str(exception)))

        # We log a 'message sent with exception' event as well as the full exception itself
        log_message_sent(exception=exception,
                         enqueue_date_str=enqueue_date_str,
                         priority=priority)
        analytics.exception()
Example #5
0
 def send_apns(self, alert, **kwargs):
     from push_notifications.apns import apns_send_bulk_message
     if not self.has_apns:
         return
     apns_send_bulk_message(
         registration_ids=[self.apns_device.registration_id],
         alert=alert,
         **kwargs)
def send_apns(registration_ids, message, **kwargs):
    '''
    Send a message to one or more APNS recipients

    :param registration_ids: a single or iterable collection of registration ids (APNS tokens)
    :param message: the payload to send. This is sent as the value of the 'alert' APNS key
    :param kwargs: additional APNS arguments. See push_notifications.apns._apns_sendd
    '''

    if isinstance(registration_ids, collections.Iterable):
        apns_send_bulk_message(registration_ids, message, **kwargs)
    else:
        apns_send_message(registration_ids, message, **kwargs)
Example #7
0
def _send_apns(registration_ids, message, **kwargs):
    '''
    Send a message to one or more APNS recipients

    :param registration_ids: a single or iterable collection of registration ids (APNS tokens)
    :param message: the payload to send. This is sent as the value of the 'alert' APNS key
    :param kwargs: additional APNS arguments. See push_notifications.apns._apns_sendd
    '''

    try:
        if isinstance(registration_ids, collections.Iterable):
            apns_send_bulk_message(registration_ids, message, **kwargs)
        else:
            apns_send_message(registration_ids, message, **kwargs)
    except Exception as exception:
        logger.info("Exception sending APNS message: %s" % str(exception))
Example #8
0
    def create(self, validated_data):
        apns = validated_data.get('apns')
        gcm = validated_data.get('gcm')
        payload = validated_data.get('payload', {})
        aps = payload.pop('aps', {})
        alert = aps.pop('alert', {})

        if apns:
            apns_send_bulk_message(registration_ids=[apns],
                                   alert=alert,
                                   extra=payload,
                                   **aps)
        if gcm:
            gcm_send_bulk_message(registration_ids=[gcm], data=payload)

        return True
Example #9
0
        #This will not hit any time.. should replace with phone number or user name and replace device keys with usernames
        devices = data['device_id']
        if web:
            devices = list(APNSDevices.objects.filter(p12_certificate=p12_certificate, registration_id__in=devices).values_list('registration_id', flat='true'))
        else:
            for device in devices:
                try:
                    #Handle length of registration key
                    #store some source name in the model
                    dev = APNSDevices.objects.get(registration_id=device)
                except:
                    dev = APNSDevices.objects.create(registration_id=device)
                    dev.save()
    if devices:
        try:
            apns_send_bulk_message(registration_ids=devices, data=PN_message)
            response = render_to_response('bulk_PN_status.html', {'status': "Successfully sent Push Notifications to all the registered users!" })
        except:
            raise
            response = render_to_response('bulk_PN_status.html', {'status': "Something went wrong and unable to send the push notifications!" })
    else:
        response = render_to_response('bulk_PN_status.html', {'status': "No registered users for the application you've opted!" })
    if not web:
        result = {'status': "Successfully sent Push Notifications to all the registered users!" } 
        return HttpResponse(json.dumps(result), content_type='application/json')
    return response


@csrf_exempt
def urbanairship(request, *args, **kwargs):
    result = ''