-
-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WebPush support for Safari #674
Conversation
c91730c
to
e1234f9
Compare
f01b5b3
to
4fb64be
Compare
I also thought about this aspect some time ago when I added push support and think this is a really good idea. This could also remove the need to extract the browser type via javascript, because we then already have the url of the endpoint we need to send the push to stored in the WebPushDevice object. |
iOS 16.4 just landed. What's the current status here? Are there still things that need to be done? |
From my prespective, as a first time contributor, this is ready to go. |
I am not a maintainer, but I could take this for a test spin. Do you know who could do a release? Maybe @alenzeinolov or @jamaalscarlett can help out? |
return results | ||
except WebPushException as e: | ||
if e.response is not None and e.response.status_code in [404, 410]: | ||
results["failure"] = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get an error here because the exception comes from line 34, but results
is defined on line 41. It should be enough to just switch them around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent point, commit added to address it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment explaining the two status codes and why you are disabling the device
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about?
results["failure"] = 1 | |
# if we find that the status from the vendor was 404 (invalide url) or 410 (Gone) we should no | |
# longer try to send messages to this device. | |
# https://web.dev/push-notifications-common-issues-and-reporting-bugs/#http-status-codes | |
results["failure"] = 1 |
Hi, is there any update please? |
Hey guys. Any updates on this? Can we do one more push and have it released? Thanks in advance. |
- ``WP_PRIVATE_KEY``: Absolute path to your private certificate file: os.path.join(BASE_DIR, "private_key.pem") | ||
- ``WP_CLAIMS``: Dictionary with the same sub info like claims file: {'sub': "mailto: development@example.com"} | ||
- ``WP_ERROR_TIMEOUT``: The timeout on WebPush POSTs. (Optional) | ||
- ``WP_POST_URL``: A dictionary (key per browser supported) with the full url that webpush notifications will be POSTed to. (Optional) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this line removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This MR is essentially deprectating the old approach. Instead the full endpoint that the browser returns is stored in the registration_id of the model, instead of just the token, and the code uses exactly what the browser gave us when we send the message. Rather than composing a url based on what is in WP_POST_URL.
As a result I thought it was sensible to remove the WP_POST_URL from the docs as it would confuse new users.
|
||
.. code-block:: python | ||
|
||
pip install py-vapid (Only for generating key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are missing the
pip install pywebpush
line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part of the docs is explaining how to generate the keys needed. It's my undestanding that you only need py-vapid for that part.
You could essentially do that part on your local machine, and then deploy your keys to production where the library is used in your application.
I tested this branch on several browsers, including Safari on iOS and I confirm this works! |
For what it worth, instead of manually using py-vapid, I could do the key generation dynamically with: from py_vapid import b64urlencode
from py_vapid import Vapid02 as Vapid
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import ec
def vapid_public_key(path):
vapid = Vapid.from_file(path)
pk = vapid.public_key.public_bytes(
serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint
)
return b64urlencode(pk)
def generate_private_key(path):
private_key = ec.generate_private_key(ec.SECP256R1, default_backend())
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
with open(path, "wb") as file:
file.write(private_pem)
file.close()
|
@blighj looks like there are some newly failing tests |
ee269cd
to
e1cb61b
Compare
Declare results variable at the start of the block
Co-authored-by: Cuyler Stuwe <salembeats@gmail.com>
Co-authored-by: James Bligh <618250+blighj@users.noreply.github.com>
• Added example code to register WP device • Fixed issue where call to extract UserAgent didn't include UA • Added examples on how to send a Web Push message
for more information, see https://pre-commit.ci
e1cb61b
to
b1e43f6
Compare
b1e43f6
to
df6998f
Compare
Codecov Report
@@ Coverage Diff @@
## master #674 +/- ##
==========================================
+ Coverage 68.67% 69.51% +0.84%
==========================================
Files 26 26
Lines 1111 1122 +11
Branches 243 245 +2
==========================================
+ Hits 763 780 +17
+ Misses 310 304 -6
Partials 38 38
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blighj looks like there are some newly failing tests
The tests are passing now, this file was failing, it is really outside the scope of this PR. My diagnosis is that a string is more bytes in py3 than the tests were expecting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done more digging and I see clearly now why the test was failing, the serilaizer is tied to the model, the registration_id in the APNSDevice has a max_length of 200, and the test was creating a string of lenght 400 ("aE"*200).
So the update I did is appropriate.
@jamaalscarlett This MR is good to go now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to re-review and hopefully get this merged today. Great work.
* Add WebPush support for Safari * Update webpush.py based on review Declare results variable at the start of the block * Fix typo in warning Co-authored-by: Cuyler Stuwe <salembeats@gmail.com> * Update README.rst Co-authored-by: James Bligh <618250+blighj@users.noreply.github.com> * Fix mailto: space * Expanded documentation for Web Push (#558) • Added example code to register WP device • Fixed issue where call to extract UserAgent didn't include UA • Added examples on how to send a Web Push message * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Attempt to fix tests * Update README.rst --------- Co-authored-by: James Bligh <james.bligh@silvercloudhealth.com> Co-authored-by: Cuyler Stuwe <salembeats@gmail.com> Co-authored-by: Éloi Rivard <eloi.rivard@nubla.fr> Co-authored-by: Neil Littlejohns <n.littlejohns@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add WebPush support for Safari * Update webpush.py based on review Declare results variable at the start of the block * Fix typo in warning Co-authored-by: Cuyler Stuwe <salembeats@gmail.com> * Update README.rst Co-authored-by: James Bligh <618250+blighj@users.noreply.github.com> * Fix mailto: space * Expanded documentation for Web Push (#558) • Added example code to register WP device • Fixed issue where call to extract UserAgent didn't include UA • Added examples on how to send a Web Push message * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Attempt to fix tests * Update README.rst --------- Co-authored-by: James Bligh <james.bligh@silvercloudhealth.com> Co-authored-by: Cuyler Stuwe <salembeats@gmail.com> Co-authored-by: Éloi Rivard <eloi.rivard@nubla.fr> Co-authored-by: Neil Littlejohns <n.littlejohns@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
PR for #673
Stores the full endpoint url for web push given by the browser, in the registration_id of the model, instead of just the token, and use exactly what the browser gave us when we send the message. Still supports existing registration_id/browser combo's but rasies a warning.
Also:
The new docs address issues/questions raised in #647, #658, #665
The PR was inspired/informed by #487, #558, #602, #603, #604, #605