diff --git a/apns.py b/apns.py index a1a8b68..ac1a9b2 100644 --- a/apns.py +++ b/apns.py @@ -84,6 +84,7 @@ class Payload: return f"{COMMANDS[self.command]}: {self.fields}" import courier +from hashlib import sha1 class APNSConnection(): def __init__(self, token: bytes=None, private_key=None, cert=None): @@ -108,6 +109,13 @@ class APNSConnection(): if 3 in resp.fields.fields: self.token = resp.fields.fields[3] + def filter(self, topics: list[str]): + payload = Payload(9, Fields({1: self.token, 2: b"".join([sha1(topic.encode()).digest() for topic in topics])})) + + self.sock.write(payload.to_bytes()) + + + if __name__ == "__main__": import courier import base64 diff --git a/demo.py b/demo.py index a6b90e3..4b82754 100644 --- a/demo.py +++ b/demo.py @@ -8,11 +8,8 @@ print(f"Push Token 1: {b64encode(conn1.token).decode()}") conn2 = apns.APNSConnection() print(f"Push Token 2: {b64encode(conn2.token).decode()}") -# Create a topic filter payload with SHA1 hash of the topic -payload = apns.Payload(9, apns.Fields({1: conn2.token, 2: sha1(b"com.apple.madrid").digest()})) -# Send the payload -#conn1.sock.write(payload.to_bytes()) -conn2.sock.write(payload.to_bytes()) +conn1.filter(["com.apple.madrid"]) +conn2.filter(["com.apple.madrid"]) #print(sha1(b"com.apple.madrid").digest()) # Send a notification