[updated] Webpush Notifications using ONLY Anvil and Python

Good afternoon,

I have been banging my head against the wall for several days trying to get webpush notifications to work on desktop and mobile (ios).

Now i FINALLY got it to work so i am happy to share the application!

Link to live app https://webpush.anvil.app

All code is Python, hence the source code is javascript, but is now “converted” and modified to python and anvil.

To get notifications you simply press the subscribe button and you will get prompted with YES/NO to turn on notifications.
If you want to “reset” the prompting for notifications, press the SSL lock, to the left side of the address bar (at least using Brave on desktop).

If you want to send a message to all subscribers you simply press the send message button.

I have tested the notifications on MacOS using the latest version of Safari, Brave and Google Chrome.
On mobile i have only tested on an iphone 8 and IOS 16.4 using safari. In safari it is important that the website is added to Homescreen.
If you don’t get any notifications on your IOS device you might check out this link https://www.xda-developers.com/how-enable-safari-notifications-iphone/. Since Apple recently released this feature on IOS.

Link to project Anvil | Login

PS
I have updated the links to the project and the live app to a more “stable” version :slight_smile:
I would very much appreciate feedback using other devices and browsers :slight_smile:

Also, there are much more to explore regarding the notification. E.g icon, sound, vibration

8 Likes

Fascinated by this! Going to have a dig in over the next couple of days!

Awesome, thanks for sharing!

It works on Chrome on my Windows PC and on my Pixel 7 Pro.

In safari it is important that the website is added to Homescreen.

Does this mean that the app must be installed as a PWA in order to work on IOS/Safari?

This is great - we should think about adding it to anvil-labs service worker implementation.


One anvil.js coding point is that you don’t need to do anvil.js.await_promise in your example.

If you call a javascript function in python that returns a promise in javascript it will be awaited.

You only ever need anvil.js.await_promise with some more obscure javascript libraries/apis.
Specifically places where you access a Promise stored as a property on an object
e.g. this browser api for animation.

Where you access the finished property which is a Promise.

We use await_promise in the anvil_extras animation module to use this api.


Other note - be careful with your uninstall method
It looks like you’re uninstalling all service worker registrations
This includes the default registration from anvil which handles offline capabilities

the following might be better

        registrations = self.navigator.serviceWorker.getRegistrations()
        for reg in registrations:
            if reg.scope.endswith("_/theme/"):
                reg.unregister()
                self.debug_log(reg)
2 Likes

Wonderful :slight_smile:

Yes that’s correct. Otherwise it seems that the service worker cannot be added.

1 Like

@stucork Thanks for your feedback!

I immediately fixed the unregistration error :slight_smile:

Thanks for the explanation regarding .await_promise. It is very new to me so i will look in to it.

Also, feel free to copy the code if it is useful in the anvil-labs :slight_smile:

2 Likes

Did you get any sound (default notification sound) and vibration on your phone?

On iPhone and Safari it seems to be a bug, that there is no sound and vibration on notifications from PWAs

Yes, I did get the default notification sound.

2 Likes

This looks great, what push provider did you use?

I’m keen to copy this and try some variations, I want to send pushes to individuals rather than all, and don’t think I should be using your API key. Just in case I do something that upsets the provider - I don’t expect to but stuff happens.

As i understand in the source documentation, the code actually uses apple, google etc as providers. No need to setup a firebase account.

The one thing needed is the vapid-keys which you can generate here: https://vapidkeys.com/

Sorry its a bit undocumented. But please replace the following:

  • ServerModule1 => Line 38 => vapid_private_key=“your own private key”
  • ServerModule1 => Line 40 => “sub”: “mailto:your email address”
  • Form1 => Line 24 => self.app_server_key = “your public key”

Thanks Tobias. I’ll give that a try as soon as work settles down a bit.

Regards

Very cool…

I have opened the app on Chrome on MacOS and allowed notifications, however I did not get the notification on my Mac after pressing “Send Notification” from a different device.

On Android I subscribed and got a browser notification even without installing the PWA.

I know iOS is very restrictive to PWAs and I think still it is necessary to add to Home Screen to get notifications?
However is it also necessary to install the app as a PWA in MacOS to get the notifications?

Hi mark!

Been a long time sense I tested it on my Mac.
But I am quite sure that I got it to work on Safari, Firefox, brave. Not sure about chrome.

Nevertheless have you tried any other browser on your Mac?

It isn’t a requirement that you add the app as a PWA. The only important thing is that you allow the notification to be sent (when you subscribe).

From my experience tinkering with push notifications - it seems to be a lack of support for this type of functionality cross devices and browsers :frowning: which is sad

Hi Tobias,

Did you remove the app lately? It doesn’t seem to be working anymore. I was hoping to do some more testing with it since the iOS functionality has changed recently, I think.

Yeah, someone was going bananas with the notifications :slight_smile:

But I’ll publish it again :slight_smile:

Sorry :smile: might have been me

Haha no problems :wink:

On the bright side, it’s good to know that we can have push notifications without firebase with anvil :slight_smile:

Yes I tried it, and it’s impressive! But it doesn’t seem to work when the webapp is translated with a webtonative solution. Did you try this?

Sorry, can you explain what webtonative solution is? Do you mean mobile a mobile app? :slight_smile:

I’m no expert but what I know is that it is a native app (app store app), that is running a webapp. Some kind of wrapper. The difference to a installable webapp like anvil is that you can publish it to the app store as a native app.

Downside is that your app did not work, I remember that some thinks in the serviceworker where not available.

You can create a free android app with this link. No need to pay, it will expire in a couple of weeks.