|
|
|
@ -2,7 +2,8 @@ import http.client |
|
|
|
import json |
|
|
|
import json |
|
|
|
import jwt |
|
|
|
import jwt |
|
|
|
import time |
|
|
|
import time |
|
|
|
from hyper import HTTP20Connection |
|
|
|
import httpx |
|
|
|
|
|
|
|
import asyncio |
|
|
|
|
|
|
|
|
|
|
|
# APPLE WARNING: Reuse a connection as long as possible. |
|
|
|
# APPLE WARNING: Reuse a connection as long as possible. |
|
|
|
# In most cases, you can reuse a connection for many hours to days. |
|
|
|
# In most cases, you can reuse a connection for many hours to days. |
|
|
|
@ -35,11 +36,12 @@ bundle_id = 'app.padelclub' |
|
|
|
# device_token = 'user_device_token' |
|
|
|
# device_token = 'user_device_token' |
|
|
|
message = 'Hello, World!' |
|
|
|
message = 'Hello, World!' |
|
|
|
|
|
|
|
|
|
|
|
def send_push_notification(device_token, message): |
|
|
|
async def send_push_notification(device_token, message): |
|
|
|
jwt_token = generate_jwt(key_path, key_id, team_id) |
|
|
|
jwt_token = generate_jwt(key_path, key_id, team_id) |
|
|
|
|
|
|
|
|
|
|
|
# APNs endpoint (use 'api.push.apple.com' for production) |
|
|
|
# APNs endpoint (use 'api.push.apple.com' for production) |
|
|
|
host = 'api.sandbox.push.apple.com' |
|
|
|
# host = 'api.sandbox.push.apple.com' |
|
|
|
|
|
|
|
url = f"https://api.sandbox.push.apple.com/3/device/{device_token}" |
|
|
|
|
|
|
|
|
|
|
|
payload = { |
|
|
|
payload = { |
|
|
|
"aps": { |
|
|
|
"aps": { |
|
|
|
@ -51,7 +53,7 @@ def send_push_notification(device_token, message): |
|
|
|
payload_json = json.dumps(payload) |
|
|
|
payload_json = json.dumps(payload) |
|
|
|
|
|
|
|
|
|
|
|
# Create the HTTP connection |
|
|
|
# Create the HTTP connection |
|
|
|
connection = HTTP20Connection(host, port=443) |
|
|
|
# connection = HTTP20Connection(host, port=443) |
|
|
|
|
|
|
|
|
|
|
|
# Set the headers |
|
|
|
# Set the headers |
|
|
|
headers = { |
|
|
|
headers = { |
|
|
|
@ -60,15 +62,21 @@ def send_push_notification(device_token, message): |
|
|
|
"content-type": "application/json" |
|
|
|
"content-type": "application/json" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# Send the notification |
|
|
|
async with httpx.AsyncClient(http2=True) as client: |
|
|
|
connection.request( |
|
|
|
response = await client.post(url, json=payload, headers=headers) |
|
|
|
"POST", |
|
|
|
|
|
|
|
f"/3/device/{device_token}", |
|
|
|
|
|
|
|
body=payload_json, |
|
|
|
|
|
|
|
headers=headers |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = connection.get_response() |
|
|
|
|
|
|
|
response_data = response.read() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(response.status, response.reason, response_data) |
|
|
|
# Send the notification |
|
|
|
|
|
|
|
# connection.request( |
|
|
|
|
|
|
|
# "POST", |
|
|
|
|
|
|
|
# f"/3/device/{device_token}", |
|
|
|
|
|
|
|
# body=payload_json, |
|
|
|
|
|
|
|
# headers=headers |
|
|
|
|
|
|
|
# ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# response = connection.get_response() |
|
|
|
|
|
|
|
# response_data = response.read() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if response.status_code == 200: |
|
|
|
|
|
|
|
print("Notification sent successfully!") |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
print(f"Failed to send notification: {response.status_code} {response.text}") |
|
|
|
|