I was able to reproduce an example with the pywebpush repository to make Push notification. After breaking my head to understand, I managed to do it. However, when I tried to integrate with my django project, it did not roll. I believe it's something related to the private_key.pem
file I create.
Error:
...
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/pywebpush/__init__.py", line 359, in webpush
vv = Vapid.from_string(private_key=vapid_private_key)
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/py_vapid/__init__.py", line 142, in from_string
return cls.from_der(pkey)
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/py_vapid/__init__.py", line 99, in from_der
backend=default_backend())
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/primitives/serialization.py", line 28, in load_der_private_key
return backend.load_der_private_key(data, password)
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/multibackend.py", line 323, in load_der_private_key
return b.load_der_private_key(data, password)
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1018, in load_der_private_key
password,
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1160, in _load_key
self._handle_key_loading_error()
File "/Users/myuser/.virtualenvs/myenv/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1229, in _handle_key_loading_error
raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
push.py
from pywebpush import webpush, WebPushException
from apps.user.models import User, PushNotification
from django.conf import settings
import os
def push_notification(message, user = None):
if not user:
all_users = PushNotification.objects.all()
for au in all_users:
try:
webpush(
subscription_info={
"endpoint":au.endpoint,
"keys":{
"p256dh":au.p256dh,
"auth":au.auth
}},
data=message,
vapid_private_key=os.path.join(settings.PRIVATEKEY_DIR, 'private_key.pem'),
vapid_claims={
"sub": "mailto:[email protected]",
}
)
except WebPushException as ex:
print("I'm sorry, Dave, but I can't do that: {}", repr(ex))
else:
_user = PushNotification.objects.filter(user = user)
if _user.exists():
_user = _user[0]
subinfo = {
"endpoint":_user.endpoint,
"keys":{
"p256dh":_user.p256dh,
"auth":_user.auth
}
}
print(subinfo)
try:
webpush(
subscription_info=subinfo,
data=message,
vapid_private_key=os.path.join(settings.PRIVATEKEY_DIR, 'private_key.pem'),
vapid_claims={
"sub": "mailto:[email protected]",
}
)
except WebPushException as ex:
print("I'm sorry, Dave, but I can't do that: {}", repr(ex))
I am generating the files according to the repository they indicate: vapid
The problem is that I was able to make Python work only. Now, calling that same Django python, it did not roll .. I start the dictionary it generates to check if it has any errors but no, it does not. It's the same.