braze-inc / braze-web-sdk

Public repo for the Braze Web SDK
https://www.braze.com
Other
73 stars 26 forks source link

ServiceWorker for push notifications not loading due to redirect #43

Closed andre-matulionis-ifood closed 6 years ago

andre-matulionis-ifood commented 6 years ago

SDK Version: 2.2 SDK init options:

Context

I followed the guide inside the Web SDK docs about implementing soft push prompts. After configuring the test campaign, I setup the soft push prompt code in some tags to be fired inside Google Tag Manager.

What should happen

After the InApp popup appears and the button is clicked, I expected to see the native permission request for notifications, since the example code calls appboy.registerAppboyPushMessages().

What really happens

The InApp message appears and the button click closes it, however the native permission request was not shown.

Debug information

I enabled the SDK option for logging, added some console.log to the soft push prompt code, and opened up the DevTools console. The main error is Appboy SDK Error: ServiceWorker registration failed: SecurityError: Failed to register a ServiceWorker: The script resource is behind a redirect, which is disallowed..

Tested in

In Chrome: braze-chrome

In Firefox: braze-firefox

PS: If you need more info, you can email me at andre.matulionis@ifood.com.br

froodian commented 6 years ago

Hi @andre-matulionis-ifood, thanks for reaching out.

In order for push to function properly, the service worker file must exist on the same site that the user is visiting, and should not redirect - these are security restrictions put in place by the Service Worker API. By default, the Braze SDK looks for that service worker file at https://yoursite.com/service-worker.js (and by default, per the documentation, it should contain self.importScripts('https://js.appboycdn.com/web-sdk/2.2/service-worker.js');). If you wish to place your service worker somewhere else, there is some ability to do that through the initialize method's serviceWorkerLocation option, though note the caveats at that documentation link about using that option.

From the error you pasted, it looks like on your server, that /service-worker.js location is redirecting, or possibly not found.

andre-matulionis-ifood commented 6 years ago

Ok, thanks for the fast answer, sorry for the trouble. I'll look into it

froodian commented 6 years ago

No worries - if you continue to experience problems getting it all set up, feel free to email support@braze.com and we'll help you work through it.