Skip to content
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

fix: iOS PWA push notifications #1794

Merged
merged 8 commits into from
Jan 8, 2025

Conversation

Soxasora
Copy link
Member

@Soxasora Soxasora commented Jan 4, 2025

Description

At last, the final fix of #756

1: Push notifications continue to be lost #411

This has been a lesson in patience with iOS, I must say. And also I got it really wrong with my previous fix!

Some key takeaways

  1. onPush event must result in a notification being shown1
  2. The notification must be shown in a timely manner in respect to the event2
  3. onPush thus can't be asynchronous by itself but needs something like event.waitUntil

To be able to use functions like getNotifications, setAppBadge, etc. we need to group them into an array of Promises that once resolved, will trigger showNotification; all encapsulated in event.waitUntil that will, indeed, wait for them to be resolved.1

If we don't respect those 3 keys, an async function like getNotifications will cause showNotification to happen outside of onPush event and will result in user's subscription being revoked.2

tl;dr A lot of text to say that we were losing the subscription because we used to send the notification out of the event with await getNotifications


2: Notification counts are wacky

The previous fix (#1796) enabled @ekzyis's iOS check to force counts to start from 1, it helped us provide immediate relief to iOS users that received mention counts all over the place

By meddling around the merging code, I discovered that now we don't need iOS-specific code to achieve proper counting, we already do that by injecting amount and sats into the notification payload and get it back for us to use it on the next merge.

About amount, we're gonna use it just for showing the correct count. We'll get the length of the notifications array to get how many notifications of the same tag are still open and do +1 (much like we were already doing)

Another problem was the missing support for notification filtering by tag, can confirm that iOS supports it.


3: when I click on notification replies I get sent to really old threads

The previous fix (#1801) included the url inside the merged notification, now the url is '/notifications' as previously intended but wasn't working


4: sat stacking notifications aren't replaced

While we can't replace them on iOS, values are finally correct!

Screenshots

TODO

Additional Context

onPush doesn't have the ability to access the default logger without bloating it, I used the (commented-out) logger from #1797 and added a comment that remembers us to use an HTTPS endpoint on the logger otherwise we can't access it

Didn't remove getOS that, even if unused, might be particularly useful during debugging

An observation:
There's a ultra-rare possibility that a push subscription could be revoked if we ever mess with sending notifications without a payload as the onPush event really requires a notification to be shown regardless and we drop the notification if the payload is not there

const payload = event.data?.json()
if (!payload) return

Still, we're protected by onPushSubscriptionChange -> RESUBSCRIBE in case this happens!

Checklist

Are your changes backwards compatible? Please answer below:
It doesn't mess with already present keys! I can say this is a backwards compatible solution.

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
8, from all the earlier PRs to now, I'm pretty confident about push sub retention.
I didn't test all the notifications but they follow the same behavior as before + counting/sumsat fixes

For frontend changes: Tested on mobile, light and dark mode? Please answer below:
n/a

Did you introduce any new environment variables? If so, call them out explicitly here:
No

Footnotes

  1. When requesting a push subscription you must promise that all pushes will be user visible events. This means that each push event you process must result in a user visible notification being displayed with a call to self.registration.showNotification(), src: Badging for Home Screen Web Apps 2

  2. After three push events where you fail to post a notification in a timely manner, your site's push subscription will be revoked. 2

@Soxasora Soxasora added the bug label Jan 4, 2025
@Soxasora Soxasora changed the title fix: iOS PWA push notifications track: iOS PWA push notifications Jan 5, 2025
@Soxasora Soxasora added feature new product features that weren't there before and removed feature new product features that weren't there before labels Jan 5, 2025
@Soxasora Soxasora changed the title track: iOS PWA push notifications fix: iOS PWA push notifications Jan 6, 2025
@Soxasora Soxasora force-pushed the ios_pwa_push_notifications branch from 21487fa to 261c377 Compare January 6, 2025 17:36
@Soxasora Soxasora force-pushed the ios_pwa_push_notifications branch from 11c0157 to 799412f Compare January 6, 2025 19:41
@Soxasora Soxasora force-pushed the ios_pwa_push_notifications branch from 799412f to f51869a Compare January 6, 2025 19:43
@Soxasora Soxasora marked this pull request as ready for review January 6, 2025 19:44
@Soxasora
Copy link
Member Author

Soxasora commented Jan 7, 2025

image
damn it, I time traveled without the belts, again!

edit: adjusted, sorry about it

@Soxasora Soxasora force-pushed the ios_pwa_push_notifications branch from 06e12b7 to d5d4ce8 Compare January 7, 2025 16:53
@Soxasora Soxasora force-pushed the ios_pwa_push_notifications branch from d5d4ce8 to 7015a57 Compare January 7, 2025 20:01
@huumn
Copy link
Member

huumn commented Jan 8, 2025

This is awesome. I'm so tired of this bug. I'll QA a little now and merge it soon after.

@huumn huumn merged commit 44d3748 into stackernews:master Jan 8, 2025
6 checks passed
@Soxasora
Copy link
Member Author

Soxasora commented Jan 8, 2025

I loved this adventure, thank you!
I'm going to monitor this really closely 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants