change to httpx
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import time
|
||||
import jwt
|
||||
import aiohttp
|
||||
import httpx
|
||||
import asyncio
|
||||
import configparser
|
||||
|
||||
@@ -82,7 +82,6 @@ def generate_apns_token():
|
||||
|
||||
|
||||
async def send_notification(
|
||||
session,
|
||||
device_token,
|
||||
title,
|
||||
body
|
||||
@@ -105,29 +104,44 @@ async def send_notification(
|
||||
headers = {
|
||||
"authorization": f"bearer {jwt_token}",
|
||||
"apns-topic": topic,
|
||||
"apns-push-type": "alert",
|
||||
"content-type": "application/json"
|
||||
"apns-push-type": "alert"
|
||||
}
|
||||
|
||||
url = f"{APNS_URL}/3/device/{device_token}"
|
||||
|
||||
async with session.post(
|
||||
url,
|
||||
json=payload,
|
||||
headers=headers
|
||||
) as response:
|
||||
async with httpx.AsyncClient(
|
||||
http2=True,
|
||||
timeout=30.0
|
||||
) as client:
|
||||
|
||||
if response.status != 200:
|
||||
response = await client.post(
|
||||
url,
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
error_text = await response.text()
|
||||
if response.status_code != 200:
|
||||
|
||||
print(
|
||||
f"APNS Fehler {response.status} "
|
||||
f"für {device_token}: {error_text}"
|
||||
f"APNS Fehler "
|
||||
f"{response.status_code} "
|
||||
f"für {device_token}: "
|
||||
f"{response.text}"
|
||||
)
|
||||
|
||||
else:
|
||||
|
||||
print(
|
||||
f"Push erfolgreich "
|
||||
f"an {device_token}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Senden: {e}")
|
||||
|
||||
print(
|
||||
f"Fehler beim Senden "
|
||||
f"an {device_token}: {e}"
|
||||
)
|
||||
|
||||
@app.route("/api/registerDeviceToken", methods=["POST"])
|
||||
def register_device_token():
|
||||
@@ -219,22 +233,19 @@ def notify():
|
||||
|
||||
async def send_all():
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
tasks = []
|
||||
|
||||
tasks = []
|
||||
for token in DeviceToken.query.all():
|
||||
|
||||
for token in DeviceToken.query.all():
|
||||
|
||||
tasks.append(
|
||||
send_notification(
|
||||
session=session,
|
||||
device_token=token.device_token,
|
||||
title=title,
|
||||
body=notification_text
|
||||
)
|
||||
tasks.append(
|
||||
send_notification(
|
||||
device_token=token.device_token,
|
||||
title=title,
|
||||
body=notification_text
|
||||
)
|
||||
)
|
||||
|
||||
await asyncio.gather(*tasks)
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
asyncio.run(send_all())
|
||||
|
||||
|
||||
+2
-1
@@ -5,4 +5,5 @@ flask
|
||||
flask_sqlalchemy
|
||||
aiohttp
|
||||
pyjwt
|
||||
cryptography
|
||||
cryptography
|
||||
httpx[http2]
|
||||
Reference in New Issue
Block a user