change to httpx
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import time
|
import time
|
||||||
import jwt
|
import jwt
|
||||||
import aiohttp
|
import httpx
|
||||||
import asyncio
|
import asyncio
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
@@ -82,7 +82,6 @@ def generate_apns_token():
|
|||||||
|
|
||||||
|
|
||||||
async def send_notification(
|
async def send_notification(
|
||||||
session,
|
|
||||||
device_token,
|
device_token,
|
||||||
title,
|
title,
|
||||||
body
|
body
|
||||||
@@ -105,29 +104,44 @@ async def send_notification(
|
|||||||
headers = {
|
headers = {
|
||||||
"authorization": f"bearer {jwt_token}",
|
"authorization": f"bearer {jwt_token}",
|
||||||
"apns-topic": topic,
|
"apns-topic": topic,
|
||||||
"apns-push-type": "alert",
|
"apns-push-type": "alert"
|
||||||
"content-type": "application/json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
url = f"{APNS_URL}/3/device/{device_token}"
|
url = f"{APNS_URL}/3/device/{device_token}"
|
||||||
|
|
||||||
async with session.post(
|
async with httpx.AsyncClient(
|
||||||
|
http2=True,
|
||||||
|
timeout=30.0
|
||||||
|
) as client:
|
||||||
|
|
||||||
|
response = await client.post(
|
||||||
url,
|
url,
|
||||||
json=payload,
|
json=payload,
|
||||||
headers=headers
|
headers=headers
|
||||||
) as response:
|
)
|
||||||
|
|
||||||
if response.status != 200:
|
if response.status_code != 200:
|
||||||
|
|
||||||
error_text = await response.text()
|
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"APNS Fehler {response.status} "
|
f"APNS Fehler "
|
||||||
f"für {device_token}: {error_text}"
|
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:
|
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"])
|
@app.route("/api/registerDeviceToken", methods=["POST"])
|
||||||
def register_device_token():
|
def register_device_token():
|
||||||
@@ -219,15 +233,12 @@ def notify():
|
|||||||
|
|
||||||
async def send_all():
|
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(
|
tasks.append(
|
||||||
send_notification(
|
send_notification(
|
||||||
session=session,
|
|
||||||
device_token=token.device_token,
|
device_token=token.device_token,
|
||||||
title=title,
|
title=title,
|
||||||
body=notification_text
|
body=notification_text
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ flask_sqlalchemy
|
|||||||
aiohttp
|
aiohttp
|
||||||
pyjwt
|
pyjwt
|
||||||
cryptography
|
cryptography
|
||||||
|
httpx[http2]
|
||||||
Reference in New Issue
Block a user