fix: iOS PWA push notifications #1794
Open
+76
−115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
onPush
event must result in a notification being shown1onPush
thus can't be asynchronous by itself but needs something likeevent.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 triggershowNotification
; all encapsulated inevent.waitUntil
that will, indeed, wait for them to be resolved.1If we don't respect those 3 keys, an async function like
getNotifications
will causeshowNotification
to happen outside ofonPush
event and will result in user's subscription being revoked.2tl;dr A lot of text to say that we were sending the notification out of the event because of
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
andsats
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)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
It contains commented-out logger and getOS that even if unused, might be particularly useful during debugging
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
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
After three push events where you fail to post a notification in a timely manner, your site's push subscription will be revoked. ↩ ↩2