aioapns icon indicating copy to clipboard operation
aioapns copied to clipboard

certificaton error when send push notification

Open gb0519 opened this issue 1 year ago • 1 comments

I use the following codes to send push notification with certification. These codes are from "base usage" of the readme file (removed the key part)

import asyncio
from uuid import uuid4
from aioapns import APNs, NotificationRequest, PushType

async def run():
    apns_cert_client = APNs(
        client_cert='mycert.cer',
        use_sandbox=False,
    )
    request = NotificationRequest(
        device_token='<DEVICE_TOKEN>',
        message = {
            "aps": {
                "alert": "Hello from APNs",
                "badge": "1",
            }
        },
        notification_id=str(uuid4()),  # optional
        time_to_live=3,                # optional
        push_type=PushType.ALERT,      # optional
    )
    await apns_cert_client.send_notification(request)
   
loop = asyncio.get_event_loop()
loop.run_until_complete(run())

The certification I generated and downloaded from developer.apple.com is cer format,
if I use it directly as client_cert='mycert.cer', Then I will get error: ssl.SSLError: [SSL] PEM lib (_ssl.c:3900)

If I convert it to pem with code: openssl x509 -inform der -in mycert.cer -out mycert.pem Then change code to client_cert='mycert.pem', Then I will get error: ssl.SSLError: [SSL] PEM lib (_ssl.c:3921)

The full traceback:

Traceback (most recent call last):
  File "/root/PythonDoc/study/testAPNs.py", line 25, in <module>
    loop.run_until_complete(run())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/root/PythonDoc/study/testAPNs.py", line 6, in run
    apns_cert_client = APNs(
  File "/usr/local/lib/python3.10/dist-packages/aioapns/client.py", line 37, in __init__
    self.pool = APNsCertConnectionPool(
  File "/usr/local/lib/python3.10/dist-packages/aioapns/connection.py", line 455, in __init__
    self.ssl_context.load_cert_chain(cert_file)
ssl.SSLError: [SSL] PEM lib (_ssl.c:3921)

What should I do to get the right certification to make it work?

gb0519 avatar May 30 '24 10:05 gb0519

@gb0519 this guide should be helpful for you how to generate correct signing files: https://github.com/node-apn/node-apn/wiki/Preparing-Certificates

edvinasbartkus avatar Aug 20 '24 12:08 edvinasbartkus