示例#1
0
def send(token, message, **kwargs):
    """
    Site: https://apple.com
    API: https://developer.apple.com
    Desc: iOS notifications
    """
    identifier = kwargs.pop('identifier', 0)
    expiry = kwargs.pop('expiry', 0)

    alert = {
        "title": kwargs.pop("event"),
        "body": message,
        "action": kwargs.pop(
            'apns_action', defaults.APNS_PROVIDER_DEFAULT_ACTION)
    }

    data = {
        "aps": {
            'alert': alert,
            'content-available': kwargs.pop('content_available', 0) and 1
        }
    }
    data['aps'].update(kwargs)
    payload = dumps(data, separators=(",", ":"), sort_keys=True).encode("utf-8")

    priority = 10

    frame = _apns_pack_frame(token, payload, identifier, expiry, priority)

    from dbmail.defaults import APNS_GW_PORT, APNS_GW_HOST
    with closing(_apns_create_socket((APNS_GW_HOST, APNS_GW_PORT))) as socket:
        socket.write(frame)
        _apns_check_errors(socket)

    return True
示例#2
0
def _apns_send(token, alert, badge=0, sound="chime", content_available=False, action_loc_key=None, loc_key=None,
               loc_args=[], extra={}, socket=None):
    data = {}

    if action_loc_key or loc_key or loc_args:
        alert = {"body": alert}
        if action_loc_key:
            alert["action-loc-key"] = action_loc_key
        if loc_key:
            alert["loc-key"] = loc_key
        if loc_args:
            alert["loc-args"] = loc_args

    data["alert"] = alert

    if badge:
        data["badge"] = badge

    if sound:
        data["sound"] = sound

    if content_available:
        data["content-available"] = 1

    data.update(extra)

    # convert to json, avoiding unnecessary whitespace with separators
    payload = json.dumps({"aps": data}, separators=(",", ":"))

    numBytes = len(payload)
    if numBytes > APNS_MAX_NOTIFICATION_SIZE:
        overflow = numBytes - APNS_MAX_NOTIFICATION_SIZE + 3
        notificationText = data['alert']
        shortenedText = notificationText[:overflow*-1]
        shortenedText += "..."
        data['alert'] = shortenedText
        payload = json.dumps({"aps": data}, separators=(",", ":"))

        if len(payload) > APNS_MAX_NOTIFICATION_SIZE:
            raise APNSDataOverflow("Notification body cannot exceed %i bytes" % APNS_MAX_NOTIFICATION_SIZE)

    data = _apns_pack_message(token, payload)

    if socket:
        socket.write(data)
        #data = socket.recv(4096)
        #print "received message:", data
    else:
        socket = _apns_create_socket()
        socket.write(data)
        socket.close()
示例#3
0
def _apns_send(
    token,
    alert,
    badge=0,
    sound="chime",
    content_available=False,
    action_loc_key=None,
    loc_key=None,
    loc_args=[],
    extra={},
    socket=None,
):
    data = {}

    if action_loc_key or loc_key or loc_args:
        alert = {"body": alert}
        if action_loc_key:
            alert["action-loc-key"] = action_loc_key
        if loc_key:
            alert["loc-key"] = loc_key
        if loc_args:
            alert["loc-args"] = loc_args

    data["alert"] = alert

    if badge:
        data["badge"] = badge

    if sound:
        data["sound"] = sound

    if content_available:
        data["content-available"] = 1

    data.update(extra)

    # convert to json, avoiding unnecessary whitespace with separators
    data = json.dumps({"aps": data}, separators=(",", ":"))

    if len(data) > APNS_MAX_NOTIFICATION_SIZE:
        raise APNSDataOverflow("Notification body cannot exceed %i bytes" % (APNS_MAX_NOTIFICATION_SIZE))

    data = _apns_pack_message(token, data)

    if socket:
        socket.write(data)
    else:
        socket = _apns_create_socket()
        socket.write(data)
        socket.close()
def _apns_send(token,
               alert,
               badge=0,
               sound="chime",
               content_available=False,
               action_loc_key=None,
               loc_key=None,
               loc_args=[],
               extra={},
               socket=None):
    data = {}

    if action_loc_key or loc_key or loc_args:
        alert = {"body": alert}
        if action_loc_key:
            alert["action-loc-key"] = action_loc_key
        if loc_key:
            alert["loc-key"] = loc_key
        if loc_args:
            alert["loc-args"] = loc_args

    data["alert"] = alert

    if badge:
        data["badge"] = badge

    if sound:
        data["sound"] = sound

    if content_available:
        data["content-available"] = 1

    data.update(extra)

    # convert to json, avoiding unnecessary whitespace with separators
    data = json.dumps({"aps": data}, separators=(",", ":"))

    if len(data) > APNS_MAX_NOTIFICATION_SIZE:
        raise APNSDataOverflow("Notification body cannot exceed %i bytes" %
                               (APNS_MAX_NOTIFICATION_SIZE))

    data = _apns_pack_message(token, data)

    if socket:
        socket.write(data)
    else:
        socket = _apns_create_socket()
        socket.write(data)
        socket.close()
示例#5
0
def _apns_send(token, data, badge=0, sound="chime", content_available=False, custom_params={}, action_loc_key=None,
               loc_key=None, loc_args=[], socket=None):
    # data = {}
    #alert = {}

    #	if action_loc_key or loc_key or loc_args:
    #		alert = {} #{"body": alert}
    #		if action_loc_key:
    #			alert["action-loc-key"] = action_loc_key
    #		if loc_key:
    #			alert["loc-key"] = loc_key
    #		if loc_args:
    #			alert["loc-args"] = loc_args

    #data["alert"] = alert

    #	if badge:
    #		data["badge"] = badge
    #
    #	if sound:
    #		data["sound"] = sound
    #
    #	if content_available:
    #		data["content-available"] = 1

    # convert to json, avoiding unnecessary whitespace with sepatators
    #data = json.dumps({"aps": data, "content": content}, separators=(",",":"))
    #data = json.dumps(data, separators=(",",":"))
    if len(data) > APNS_MAX_NOTIFICATION_SIZE:
        raise APNSDataOverflow("Notification body cannot exceed %i bytes" % (APNS_MAX_NOTIFICATION_SIZE))

    data = _apns_pack_message(token, data)

    if socket:
        socket.write(data)
    else:
        socket = _apns_create_socket()
        socket.write(data)
        socket.close()