Anvil with OneSignal (JS) for Push Notifications/Web Workers

What I’m trying to do:
I am trying to use the OneSignal javascript library along with anvil’s background tasks to enable push messaging in my app. I have imported the javascript libraries in ‘Native Libraries’. My requests are logged in the server with code 200, indicating that everything is working fine on OneSignal’s end. The notifications permissions popup also works fine.

But, the web worker is not registering properly. The problem arises when trying to get the push notifications client-side.

When debugging I get an error that Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘https://anvil.works’) does not match the recipient window’s origin (‘https://xxxxx.anvil.app’).

It was as simple as editing the settings in OneSignal.com to pull the js files from the themes directory.

/your_app_URL_here/_/theme/

1 Like

I am not having any luck getting this to work. Can you provide a little more clarity? The only options I see in the OneSignal.com settings are:
“Path to service worker files”
“Service worker registration scope”

I have tried updating them to “/_/theme/” and that does not seem to work. When I run my app; In the inspection console I see this message:

'The FetchEvent for "https://cdn.onesignal.com/sdks/OneSignalSDK.js" resulted in a network error response: the promise was rejected.'

And the welcome notification never shows?

Maybe this has now changed since the update of Anvil to a full PWA in recent months?

Try to update it like the following:

The blacked-out portion is your unique app url string

Thanks, I tried that and got the same result?

Here is what I’m seeing in the browser inspection console:

I had once successfully implemented One Signal in my Anvil app. Although I removed it due to other reasons.

I’ll try again and let you know if I have any success.

It looks like it is browser-specific. It appears to work in Edge and Firefox, but not in Google or Brave.

Hello, hope you are fine, anyone have success to integrate onesignal on anvil ?

Hey Divyesh,

Did you ever get OneSignal properly integrated with your app again? I am having the same error as @chad63e which appears to stem from multiple service workers, and since we don’t have access to the Anvil SW, I cannot merge them.

I didn’t try it out again. I started using Firebase which I found to answer my needs better.

I created a library for it - Push Notifications Dependency. However, while it still works, the current version of the library is outdated as per Google changes.

You can try this one out if you want to use Firebase. It uses the current supported version from Google.

Do you know it works with native push on iOS / Android? I think if there is no .p12 file used for registering on iOS you won’t be able to send push notifications when the app is closed.

The push notifications work well on Android. It previously had issues with IOS but looks like they have added the functionality for it now.

Ok, I will try it again, thanks.