-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
868 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
from binascii import a2b_hex | ||
from json import dumps | ||
from socket import socket, AF_INET, SOCK_STREAM | ||
from struct import pack | ||
from time import time | ||
|
||
try: | ||
from ssl import wrap_socket | ||
except ImportError: | ||
from socket import ssl as wrap_socket | ||
|
||
from django.conf import settings | ||
|
||
from dbmail.providers.apple.errors import APNsError | ||
from dbmail import PY3 | ||
|
||
|
||
def send(token_hex, message, **kwargs): | ||
""" | ||
Site: https://apple.com | ||
API: https://developer.apple.com | ||
Desc: iOS notifications | ||
""" | ||
is_enhanced = kwargs.pop('is_enhanced', False) | ||
identifier = kwargs.pop('identifier', 0) | ||
expiry = kwargs.pop('expiry', 0) | ||
data = { | ||
"aps": { | ||
'alert': message, | ||
'content-available': kwargs.pop('content_available', 0) and 1 | ||
} | ||
} | ||
data['aps'].update(kwargs) | ||
payload = dumps( | ||
data, separators=(',', ':'), ensure_ascii=False).encode('utf-8') | ||
|
||
token = a2b_hex(token_hex) | ||
if is_enhanced is True: | ||
fmt = '!BIIH32sH%ds' % len(payload) | ||
expiry = expiry and time() + expiry | ||
notification = pack( | ||
fmt, 1, identifier, expiry, | ||
32, token, len(payload), payload) | ||
else: | ||
token_length_bin = pack('>H', len(token)) | ||
payload_length_bin = pack('>H', len(payload)) | ||
zero_byte = bytes('\0', 'utf-8') if PY3 is True else '\0' | ||
notification = ( | ||
zero_byte + token_length_bin + token + | ||
payload_length_bin + payload) | ||
|
||
sock = socket(AF_INET, SOCK_STREAM) | ||
sock.settimeout(3) | ||
sock.connect((settings.APNS_GW_HOST, settings.APNS_GW_PORT)) | ||
ssl = wrap_socket( | ||
sock, settings.APNS_KEY_FILE, | ||
settings.APNS_CERT_FILE, | ||
do_handshake_on_connect=False) | ||
|
||
result = ssl.write(notification) | ||
|
||
sock.close() | ||
ssl.close() | ||
|
||
if not result: | ||
raise APNsError | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from json import dumps | ||
|
||
from django.conf import settings | ||
from hyper import HTTP20Connection | ||
from hyper.tls import init_context | ||
|
||
from dbmail.providers.apple.errors import APNsError | ||
|
||
|
||
def send(token_hex, message, **kwargs): | ||
""" | ||
Site: https://apple.com | ||
API: https://developer.apple.com | ||
Desc: iOS notifications | ||
Installation and usage: | ||
pip install hyper | ||
""" | ||
|
||
priority = kwargs.pop('priority', 10) | ||
topic = kwargs.pop('topic', None) | ||
data = { | ||
"aps": { | ||
'alert': message, | ||
'content-available': kwargs.pop('content_available', 0) and 1 | ||
} | ||
} | ||
data['aps'].update(kwargs) | ||
payload = dumps( | ||
data, separators=(',', ':'), ensure_ascii=False).encode('utf-8') | ||
|
||
headers = { | ||
'apns-priority': priority | ||
} | ||
if topic is not None: | ||
headers['apns-topic'] = topic | ||
|
||
ssl_context = init_context() | ||
ssl_context.load_cert_chain(settings.APNS_CERT_FILE) | ||
connection = HTTP20Connection( | ||
settings.APNS_GW_HOST, settings.APNS_GW_PORT, ssl_context=ssl_context) | ||
|
||
stream_id = connection.request( | ||
'POST', '/3/device/{}'.format(token_hex), payload, headers) | ||
response = connection.get_response(stream_id) | ||
if response.status != 200: | ||
raise APNsError(response.read()) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class APNsError(Exception): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
import hmac | ||
from hashlib import sha256 | ||
from json import dumps | ||
from urllib import urlencode | ||
from urllib2 import urlopen, Request | ||
|
||
from django.conf import settings | ||
|
||
|
||
class CentrifugoError(Exception): | ||
pass | ||
|
||
|
||
def send(channel, data, **kwargs): | ||
if type(data) not in (set, dict, list, tuple): | ||
kwargs['message'] = data | ||
|
||
data = dumps([{ | ||
"method": "publish", | ||
"params": { | ||
"channel": channel, | ||
"data": kwargs, | ||
}, | ||
}]) | ||
sign = hmac.new(settings.CENTRIFUGO_TOKEN, data, sha256).hexdigest() | ||
payload = urlencode({'data': data, 'sign': sign}) | ||
|
||
response = urlopen( | ||
Request(settings.CENTRIFUGO_API), | ||
payload, | ||
timeout=10 | ||
) | ||
if response.code != 200: | ||
raise CentrifugoError(response.read()) | ||
return True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
from httplib import HTTPSConnection | ||
from json import dumps, loads | ||
from urlparse import urlparse | ||
|
||
from django.conf import settings | ||
|
||
|
||
class GCMError(Exception): | ||
pass | ||
|
||
|
||
def send(user, message, **kwargs): | ||
""" | ||
Site: https://developers.google.com | ||
API: https://developers.google.com/cloud-messaging/ | ||
Desc: Android notifications | ||
""" | ||
|
||
headers = { | ||
"Content-type": "application/json", | ||
"Authorization": "key=" + kwargs.pop("gcm_key", settings.GCM_KEY) | ||
} | ||
|
||
hook_url = 'https://android.googleapis.com/gcm/send' | ||
|
||
data = { | ||
"registration_ids": [user], | ||
"data": { | ||
"title": kwargs.pop("event"), | ||
'message': message, | ||
} | ||
} | ||
data['data'].update(kwargs) | ||
|
||
up = urlparse(hook_url) | ||
http = HTTPSConnection(up.netloc) | ||
http.request( | ||
"POST", up.path, | ||
headers=headers, | ||
body=dumps(data)) | ||
response = http.getresponse() | ||
|
||
if response.status != 200: | ||
raise GCMError(response.reason) | ||
|
||
body = response.read() | ||
if loads(body).get("failure") > 0: | ||
raise GCMError(repr(body)) | ||
return True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
from httplib import HTTPConnection, HTTPSConnection | ||
from urlparse import urlparse | ||
from json import dumps | ||
|
||
from django.conf import settings | ||
|
||
|
||
class HTTPError(Exception): | ||
pass | ||
|
||
|
||
def send(hook_url, message, **kwargs): | ||
headers = { | ||
"Content-type": "application/json" | ||
} | ||
|
||
http_key = kwargs.pop("http_key", None) | ||
if not http_key and hasattr(settings, 'HTTP_KEY'): | ||
http_key = settings.HTTP_KEY | ||
headers["Authorization"] = "key={}".format(http_key) | ||
|
||
kwargs["msg"] = message | ||
|
||
up = urlparse(hook_url) | ||
if up.scheme == 'https': | ||
http = HTTPSConnection(up.netloc) | ||
else: | ||
http = HTTPConnection(up.netloc) | ||
|
||
http.request( | ||
"POST", up.path, | ||
headers=headers, | ||
body=dumps(kwargs)) | ||
response = http.getresponse() | ||
|
||
if response.status != 200: | ||
raise HTTPError(response.reason) | ||
return True |
Empty file.
Oops, something went wrong.