forked from Pr0Ger/PyAPNs2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation for sending notifications
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from enum import Enum | ||
from json import dumps | ||
|
||
from hyper import HTTP20Connection | ||
from hyper.tls import init_context | ||
|
||
|
||
class NotificationPriority(Enum): | ||
Immediate = 10 | ||
Delayed = 5 | ||
|
||
|
||
class APNsClient(object): | ||
def __init__(self, cert_file, use_sandbox=False, use_alternate_port=False): | ||
server = 'api.development.push.apple.com' if use_sandbox else 'api.push.apple.com' | ||
port = 2197 if use_alternate_port else 443 | ||
ssl_context = init_context() | ||
ssl_context.load_cert_chain(cert_file) | ||
self.__connection = HTTP20Connection(server, port, ssl_context=ssl_context) | ||
|
||
def send_notification(self, token_hex, notification, topic=None): | ||
json_payload = dumps(notification.dict(), ensure_ascii=False, separators=(',',':')).encode('utf-8') | ||
headers = {} | ||
|
||
if topic: | ||
headers['apns-topic'] = topic | ||
|
||
url = '/3/device/{}'.format(token_hex) | ||
self.__connection.request('POST', url, json_payload, headers) |
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,15 @@ | ||
from typing import Optional | ||
|
||
from apns2.payload import Payload | ||
|
||
|
||
class APNsClient(object): | ||
def __init__(self, | ||
cert_file: str, | ||
use_sandbox: bool = False, | ||
use_alternate_port: bool = False) -> None: ... | ||
|
||
def send_notification(self, | ||
token_hex: str, | ||
notification: Payload, | ||
topic: Optional[str] = None) -> None: ... |
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