mirror of
https://github.com/Sneed-Group/pypush-plus-plus
synced 2024-12-24 20:02:31 -06:00
trying to send messages
This commit is contained in:
parent
6e3601de58
commit
b9d04f965a
2 changed files with 34 additions and 1 deletions
31
apns.py
31
apns.py
|
@ -23,8 +23,8 @@ def _deserialize_field(stream: bytes) -> tuple[int, bytes]:
|
||||||
value = stream[3 : 3 + length]
|
value = stream[3 : 3 + length]
|
||||||
return id, value
|
return id, value
|
||||||
|
|
||||||
|
|
||||||
# Note: Takes a stream, not a buffer, as we do not know the length of the payload
|
# Note: Takes a stream, not a buffer, as we do not know the length of the payload
|
||||||
|
# WILL BLOCK IF THE STREAM IS EMPTY
|
||||||
def _deserialize_payload(stream) -> tuple[int, list[tuple[int, bytes]]] | None:
|
def _deserialize_payload(stream) -> tuple[int, list[tuple[int, bytes]]] | None:
|
||||||
id = int.from_bytes(stream.read(1), "big")
|
id = int.from_bytes(stream.read(1), "big")
|
||||||
|
|
||||||
|
@ -87,3 +87,32 @@ class APNSConnection:
|
||||||
payload = _serialize_payload(9, fields)
|
payload = _serialize_payload(9, fields)
|
||||||
|
|
||||||
self.sock.write(payload)
|
self.sock.write(payload)
|
||||||
|
|
||||||
|
def send_message(self, token: bytes, topic: str, payload: str):
|
||||||
|
# Current time in UNIX nanoseconds
|
||||||
|
import time
|
||||||
|
# Expire in 5 minutes
|
||||||
|
expiry = int(time.time()) + 500
|
||||||
|
|
||||||
|
payload = _serialize_payload(0x0a,
|
||||||
|
[(1, sha1(topic.encode()).digest()),
|
||||||
|
(2, token),
|
||||||
|
(3, payload.encode("utf-8")),
|
||||||
|
(4, (3864024149).to_bytes(4, "big")),
|
||||||
|
(5, expiry.to_bytes(4, "big")),
|
||||||
|
(6, time.time_ns().to_bytes(8, "big")),
|
||||||
|
(7, 0x00.to_bytes()),
|
||||||
|
(0xf, self.token)])
|
||||||
|
|
||||||
|
print(payload)
|
||||||
|
|
||||||
|
self.sock.write(payload)
|
||||||
|
|
||||||
|
payload = _deserialize_payload(self.sock)
|
||||||
|
|
||||||
|
print(payload)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: Find a way to make this non-blocking
|
||||||
|
def expect_message(self) -> tuple[int, list[tuple[int, bytes]]] | None:
|
||||||
|
return _deserialize_payload(self.sock)
|
4
demo.py
4
demo.py
|
@ -13,6 +13,10 @@ print(f"Push Token 2: {b64encode(conn2.token).decode()}")
|
||||||
conn1.filter(["com.apple.madrid"])
|
conn1.filter(["com.apple.madrid"])
|
||||||
conn2.filter(["com.apple.madrid"])
|
conn2.filter(["com.apple.madrid"])
|
||||||
|
|
||||||
|
conn1.send_message(conn2.token, "com.apple.madrid", "\{'hello': 'world'\}")
|
||||||
|
|
||||||
|
#print(conn1.expect_message())
|
||||||
|
#print(conn2.expect_message())
|
||||||
# #print(sha1(b"com.apple.madrid").digest())
|
# #print(sha1(b"com.apple.madrid").digest())
|
||||||
# # Send a notification
|
# # Send a notification
|
||||||
# # expiry timestamp in UNIX epoch
|
# # expiry timestamp in UNIX epoch
|
||||||
|
|
Loading…
Reference in a new issue