[updated] Webpush Notifications using ONLY Anvil and Python

Oh, that’s super interesting, I have to look in to that project :slight_smile:
But to the issue, I would guess that the problem would be api related. Native IOS app have their own api for push notifications, not the same as safari/chrome etc. So I guess that ”my code” don’t gets translated in the correct way to handle native push notifications.

Perhaps we could get in touch with webtonative :slight_smile:

Is this like PWABuilder? I was able to convert my app into a working Android app with PWAbuilder, but trying to do the same to iOS threw a lot of errors I was unable to resolve. I heard PWABuilder is not great for iOS. Maybe this one is different?

Edit: I tried it and actually it seemed to generate the app properly for iOS (at least, it works in the simulator). The difference is that webtonative is not free.

Also, I think the reason the push notifications did not work is that you must give this as an add-on inside webtonative (when you customize the app). It costs some additional money.

By the way, the “Link to project” is still not working (presumably the clone link)

Oh, my bad!

Here’s a new link :slight_smile:

Yeah something like that! It indeed costs some money, with discount you got an Android and iOS app for 79$ total. I think that is a fare price.

The normal way for notifications is to add OneSignal, which I think is a free add-on. See “Edit … App”, “Plugin Integration”. I have tested this and it works fine. You can send a test notification by hitting a button somewhere in OnSignal. You can test this with the free version of the app. Btw. the free version, expire after a few weeks or so.

What I have not figured out yet is how to send a notification from a background task in Anvil → Firebase → OneSignal → phone. That is why I was interested in the python solution, keeping it within Anvil.

Yes, that’s also what I am thinking about. I don’t mind sending notifications from OneSignal, but then how does it know which of my users have opted-in, which are registered users / other details from my users DB? I think it would be necessary to do this from within the app to send proper and customized notifications.

Running on Chrome in iOS 17.4 gives me an error:

AttributeError: ‘ServiceWorkerRegistration’ object has no attribute ‘pushManager’

at Form1, line 70

Yes, this is indeed the problem. Maybe @tobias.carlbom understand this better?

From what I can remember, it’s only safari on iOS which has a notification api to the actual iOS device eg pushmanager.
As I understand you have to “install” the anvil webapp as a PWA (in safari) to get access to the pushmanager object.
Can you please try what? :slight_smile:

PWA:s is a bit frustrating :confused: apple and android seem to have their own limitations. Eg only certain browsers can install the PWA.

But I’ll look in to it :slight_smile: the error message is very nasty :confused:

This is great :slight_smile:

Have you done any work on getting notification badges to show up on the PWA? I’m looking to do something like that in a few months.

I managed to get OneSignal notifications work from a python call using a OneSignal Python SDK.

So the steps:

  1. Transform the Anvil webapp to a native app with webtonative.com
    1.1 Setup OneSignal when creating the app
    1.2 You need to connect firebase, but firebase is not really used
  2. download and install the app on your phone
  3. Put the following code in an Anvil app or background task to do a push notification.
from onesignal_sdk.client import Client

# Initialize OneSignal client
client = Client(app_id="your app id from Onesignal",
                          rest_api_key="rest api key from Onesignal app",
                          user_auth_key=" can be found in your personal settings")


@anvil.server.callable
def push_onesignal():
  print(client)
  try:
    notification_body = {
      'contents': {'en': 'New notification'},
        'included_segments': ['All'],
      #'filters': [],
    }
    response = client.send_notification(notification_body)
    print(response.body)

  except Exception as e:
      print(f"Error sending push notification: {e}")

This sends a push notification to all app installers. Something I can’t seem to figure out is how to get the “device id”, “player id” or “onesignal_id” (basicly whatever id that is unique to the user/install).

So that we can send push notification to certain users, and put that user id in our anvil user database.

There is another project: onesignal js and anvil but they use the js api. Which is not pure python.

There are also different onesignal python sdk’s, others I have not been able to make them work.

1 Like

Sorry for my late response, but i have some news!

I just tried out https://appilix.com/ and i converted https://servermonitoring.anvil.app/ to an actual ios app!! When i first fired up the app i could not believe my eyes.

A short but important disclaimer. Appilix has a free plan for testing only (no push notifications). I purchased the lifetime license for IOS for 49 USD, which means i can publish one IOS app to app store. For android its 29 USD.

Appilix is actually very well documented so i highly recommend trying it out!

Link to recording from my mobile.

For the push notification part, after connecting a firebase account to appilix/ios app, its possible to use appilix api. Example code which can be used in anvil server function.

import requests

# Endpoint URL
url = 'https://appilix.com/api/push-notification'

# Data to be sent in the request body
data = {
    'app_key': 'your_app_key',
    'api_key': 'your_api_key',
    'notification_title': 'Your Notification Title',
    'notification_body': 'Your Notification Body'
}

# Making the POST request
response = requests.post(url, data=data)

# Checking the response
if response.status_code == 200:
    print('Notification sent successfully!')
else:
    print('Failed to send notification. Status code:', response.status_code)

3 Likes

You may want to look here:

I think the params you are looking for are include_player_ids

1 Like

Did you get this to work?

fyi i have re-uploaded my screen recording for my last post :slight_smile:

2 Likes

You should send the push notifications from the OneSignal admin console, not from your app. You can define segmentation there and who should receive what kinds of messages (although I think more advanced segmentation requires a paid plan).
In their admin console I could easily see all of the registered devices of my app and send test messages to the ones I wanted to send to. I am still pretty new in it though, so I’m figuring out how it works. So far only using it with my iOS “native” app.

1 Like